我做了一个java ServerSocket。我使用 Socket 对象向它发送请求。请求被传递到 ServerSocket 但没有返回响应。
服务器代码:
Socket startserver = this.wifiserver.accept();
in = new InputStreamReader(startserver.getInputStream());
BufferedReader read = new BufferedReader(in);
String request = read.readLine();
//System.out.println(request);
if(request.equals("SNDKEY")){
System.out.println("Command is: SNDKEY");
out = new PrintWriter(startserver.getOutputStream());
out.print("12345678901234567890");
out.close();
}
客户代码:
Socket connection = new Socket( ip, port );
writeServer = new PrintWriter(connection.getOutputStream());
inputStream = new InputStreamReader(connection.getInputStream());
bufferStream = new BufferedReader(inputStream);
writeServer.print("SNDKEY");
this.Key = bufferStream.readLine();
问题是程序卡在命令 bufferStream.readLine() 上。我通过输出以下行命令检查了请求是否到达服务器: SNDKEY 并且它总是被打印出来。但是用户端永远不会收到密钥。