-1
服務器使用線程接受多個連接。簡單套接字通信程序根本不工作
服務器:
@Override
public void run() {
try {
System.out.println(client);
System.out.println("Client Connected");
//So far, so good. Client is connected
BufferedReader in = new BufferedReader(
new InputStreamReader(this.client.getInputStream()));
System.out.println(in.readLine());
// Nothing happens
} catch (Exception e) {
e.printStackTrace();
}
}
客戶:
try {
PrintWriter out = new PrintWriter(Client.socket.getOutputStream());
BufferedReader in = new BufferedReader (
new InputStreamReader(Client.socket.getInputStream()));
out.write("Information sent from client");
// Does not work
in.read();
// Before this .read it would give a "Connection reset" probably
// Because the client closed before the server could even read
}
沒有錯誤,但它只是掛起,沒有被髮送。防火牆已關閉,所以它不可能是這樣。它也用於昨天工作。我不知道我可能搞砸了什麼。
您是否嘗試過使用網貓來分別替換客戶端和服務器以查看哪些作品? – 735Tesla
'readline()'依賴於換行符,按照文檔。你沒有發送一個,所以它會阻止並永遠不會返回。 –