发生了什么:
Main server -> String("isalive") => Other server
Other server -> String("alive") => Main server
配置端口和内容(两台服务器都是专用机器)
和代码。东西似乎破坏的地方是(我添加了调试消息....)
reader.readLine();
主服务器代码:
Socket clientSocket = new Socket(key, 6789);
if(clientSocket.isConnected() && !clientSocket.isClosed()){
String in;
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(isalive);
outToServer.flush();
//it wont go pass this one
in = inFromServer.readLine();
if(in.equals("alive")){
if(!Data.hosts.get(key)){
Data.hosts.put(key, true);
}
}
outToServer.close();
inFromServer.close();
clientSocket.close();
它不会通过这个,因为它会停在另一台服务器上。
代码:
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
//stuck here
rec = inFromClient.readLine();
if(rec.equals("isalive")){
outToClient.writeBytes("alive");
}
inFromClient.close();
outToClient.flush();
outToClient.close();
connectionSocket.close();
提前致谢。
答案 0 :(得分:0)
您不应该组合DataOutputStream和BufferedReader。 考虑使用PrintWriter代替并指定字符编码(为了安全)。
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
writer.println("Hello.");
writer.flush();
reader.readLine();