Java套接字程序支持

时间:2014-09-20 18:02:47

标签: java networking network-programming

你好,我正在用Java做一个聊天程序,但后来我提出了想法,我想制作命令。例如,服务器写"日期"然后它将返回客户的时间和日期!

在我写的代码中:while(!streamChat.equals("CLIENT - END")); 它将关闭流或套接字,但我不能做一个if statemend所以它可以读取它是什么以及它应该做什么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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:-1)

答案很简单。你只需写:

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