我想使用 TLS 编写小型“聊天”程序
我在使用 SSL 套接字和输入流时遇到问题。TLS 部分工作正常。
我的问题是:“ bufferedreader.ready()
”总是返回false
。如果我删除 teh if -satetmant if 工作“正常”。程序一直等到有 \n 或 \r 并打印到 System.out。但我不能在那个时候发送消息。
“客户”
InputStream inputstream = System.in;
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
InputStream inputstream1 = socket.getInputStream();
InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
BufferedReader bufferedreader1 = new BufferedReader(inputstreamreader1);
OutputStream outputstream = socket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
OutputStream outputstream1 = System.out;
OutputStreamWriter outputstreamwriter1 = new OutputStreamWriter(outputstream1);
BufferedWriter bufferedwriter1 = new BufferedWriter(outputstreamwriter1);
//Mitteilen, welchen Kommprofil zum verbindugn genutzt werden soll.
bufferedwriter.write("bez\n");
while (true) {
if (bufferedreader.ready()) {
bufferedwriter.write(bufferedreader.readLine() + '\n');
bufferedwriter.flush();
}
if (bufferedreader1.ready()) {
bufferedwriter1.write(bufferedreader1.readLine() + '\n');
bufferedwriter1.flush();
}
}
“服务器”:
InputStream inputstream = sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
InputStream inputstream1 = System.in;
InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
BufferedReader bufferedreader1 = new BufferedReader(inputstreamreader1);
OutputStream outputstream = System.out;
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
OutputStream outputstream1 = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter1 = new OutputStreamWriter(outputstream1);
BufferedWriter bufferedwriter1 = new BufferedWriter(outputstreamwriter1);
while (true ) {
if (bufferedreader.ready()) {
bufferedwriter.write(bufferedreader.readLine() + '\n');
bufferedwriter.flush();
}
if (bufferedreader1.ready()) {
bufferedwriter1.write(bufferedreader1.readLine() + '\n');
bufferedwriter1.flush();
}
}