2014-09-20 61 views
-4

你好我正在用Java做一個聊天程序,但後來我想出了idee,我想做出命令。例如,服務器寫入「日期」,然後它將返回客戶端的時間和日期!Java套接字程序支持

在代碼我寫:while(!streamChat.equals("CLIENT - END")); 它將關閉流或套接字,但我不能做一個如果修改,以便它可以讀取至極指揮它是什麼,它應該做的plz幫助我。

這裏是我的代碼:

 do{ 
     try{ 
      streamChat = (String) inputdata.readObject();  //Reading the object.. and storing it in a variable... 
      showMess("\n " + streamChat); 

     }catch(ClassNotFoundException classNotFoundException){ 
      //catching the errors... 
     } 

    }while(!streamChat.equals("CLIENT - END"));  //The ending 
+0

我現在不在哪裏把if語句... ^^ – Tor 2014-09-20 18:14:19

回答

-1

答案很簡單。你只是寫:

try{ 
     streamChat = (String) inputdata.readObject();  //Reading the object.. and storing it in a variable... 
     if(//your statement here){ 
      //Your command action here 
      }   
    } 
1

它是有點不清楚你的問題,但我猜你需要到特定的字符串讀取讀取插座。

試試這個

streamChat = (String) inputdata.readObject(); 
while (!streamChat.equals("CLIENT - END")) { 

    try { 
     showMess("\n " + streamChat); 
     streamChat = (String) inputdata.readObject();  


    } catch (ClassNotFoundException classNotFoundException) { 
     //catching the errors... 
    } 

} 

closeSocket();