我正在創建一個多客戶端/服務器應用程序,無論何時客戶端從我的服務器斷開 掛起。 如何設置,會告訴我打印一些消息的任何條件,只要 從服務器 這裏的任何客戶端斷開是我的服務器代碼客戶端斷開連接時服務器掛起
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
System.out.println("server starting.......");
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("Ready to accept.......");
socket = serverSocket.accept();
System.out.println(" client Connected with ip address =" +socket.getRemoteSocketAddress().toString());
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
System.out.println("catch block");
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
任何幫助將不勝感激
掛起如何?它會停止接受任何新的連接,或者只是CommunicationThread卡住了? –
我有一個textview,我設置了從客戶端收到的所有消息。但每當任何客戶端diconnectes textview顯示客戶端說:null,這在循環中,最後我的應用程序崩潰 – Mohit
是的,只是聽某些端口接收字符串,如果您收到的東西,停止服務器:) –