2016-01-15 45 views
0
  DataInputStream dis = new DataInputStream(s.getInputStream()); 
     String Message = dis.readUTF(); 
     History.setText(History.getText()+ '\n' + "Client:-" + Message); 
     System.out.println("Message from client :- "+ Message); 
     } 
    catch(IOException e) { 

     History.setText(History.getText()+ "\n" + "Error in Connection"); 
     History.setText(History.getText()+ "\n" + "Please Try Again or Exit"); 

解決 現在能夠從客戶端和用戶無法得到的消息

+0

對於初學者,我會在IOException捕獲塊中添加日誌記錄。也許某個地方正在拋出異常。另一個觀察 - DataInputStreams和DataOutputStreams沒有被關閉。你可以使用試用資源。 – Boris

+0

「他們不能接收和發送來自對方的消息」是什麼意思?引發異常或服務器和客戶端開始等待,什麼都沒有發生? – dezhik

+0

服務器和客戶端已成功連接,但客戶端和服務器都無法在單擊發送按鈕時收到對方的消息。 – kapoor

回答

0

看來,無論是客戶端和服務器都在等待一個消息發送到對方得到輸出。你的代碼結構規定,在建立連接後,服務器等待來自客戶端的消息,

DataInputStream dis = new DataInputStream(s.getInputStream()); 
String Message = dis.readUTF(); 
History.setText(History.getText()+ '\n' + "Client:-" + Message); 
System.out.println("Message from client :- "+ Message); 

和你的客戶也在等待來自服務器的消息,

DataInputStream dis = new DataInputStream(s.getInputStream()); 
String Message = dis.readUTF(); 
History.setText(History.getText()+ '\n' + "Server:- " + Message); 

刪除任這些代碼行中,或者在等待傳入消息之前,客戶端或服務器向對方發送消息。

0

您的代碼正在工作。

在動作處理程序中,您在發送消息之前清除消息。

Msg.setText(""); 

刪除客戶端和服務器中的該行。

您仍然有問題,因爲只能接收到第一條消息。