本人编写了一个服务器,其中在接受和发送消息的地方有问题,出问题的在如下地方:
BufferedReader is = new BufferedReader(new InputStreamReader( client.getInputStream()));
String strGetMSG = is.readLine();

PrintWriter os = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), true);
String outLine; // Output server request
outLine = processInput(null);
os.println(outLine);
os.flush();
在调试的时候我从客户端发送一个字符串“hello”,is.readLine()能够读到该字符串,但是就不能调试下去了strGetMSG也得不到值,这是什么原因阿?
其中客户端处理消息的代码如下:
 public void MessageOperate(){
   try{
Socket client = new Socket(SERVERADDRESS,SERVERPORT);   
//接收消息流对象
BufferedReader brSocketReader = new BufferedReader(new InputStreamReader( client.getInputStream()));
//发送消息流对象
PrintWriter pwSocketWriter = new PrintWriter(new OutputStreamWriter(client.getOutputStream()),true);
//发送jTextSendInfo消息
pwSocketWriter.println(jTextSendInfo.getText());
//获得反馈信息       
String strGetMSG = brSocketReader.readLine();
//显示反馈信息
jTextGetInfo.setText(strGetMSG);
brSocketReader.close();
pwSocketWriter.close();
client.close();
}
catch(Exception e){
e.printStackTrace();
}
  }//MessageOperate()

解决方案 »

  1.   

    问题可能出在outLine = processInput(null);
    processInput(null);是做什么的?
      

  2.   

    processInput()是服务器收到客户端的请求后所作出的回应,即传出一个字符串,该函数代码如下:    String processInput(String inStr) {
            String outStr = null;        switch (state) {
                case WAIT_FOR_CLIENT:
                    // Ask a question
                    outStr = questions[num];
                    state = WAIT_FOR_ANSWER;
                    break;            case WAIT_FOR_ANSWER:
                    // Check the answer
                    if (inStr.equalsIgnoreCase(answers[num]))
                        outStr="\015\012That's correct! Want another (y/n)?";
                    else
                        outStr="\015\012Wrong, the correct answer is "
                            + answers[num] +". Want another (y/n)?";
                    state = WAIT_FOR_CONFIRM;
                    break;            case WAIT_FOR_CONFIRM:
                    // See if they want another question
                    if (!inStr.equalsIgnoreCase("N")) {
                        num = Math.abs(rand.nextInt()) % questions.length;
                        outStr = questions[num];
                        state = WAIT_FOR_ANSWER;
                    } else {
                        outStr = "Bye.";
                        state = WAIT_FOR_CLIENT;
                    }
                    break;
            }
            return outStr;
        }