我有經由PrintWriter的(out.write(字符串str))發送文本數據的套接字程序,在另一側的BufferedReader(in.readLine())正在接收此。中斷插座連接
我想用某種「超時」,如果一個客戶端斷開連接保護我的計劃,但我測試了它和工作,當客戶端將被關閉,服務器仍然發送數據並沒有中斷,也沒有在套接字關閉。
同一客戶端,我想打破連接並顯示例如信息「服務器不工作」。 我該如何處理?
// Server
out = new PrintWriter(sock.getOutputStream());
while(true) {
this.out.write("Hello");
this.out.flush();
}
// Client
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
while (true) {
in.readLine();
}
的readLine()返回null。 – EJP