Java网络服务器,协议和客户端

时间:2013-05-17 06:38:20

标签: java

我有这个多服务器线程,所以每当点击右上角的'X'按钮时,我想在服务器端显示消息“关闭套接字”。

public void run
{

  try
  {
    // .....
     String inputLine, outputLine;
    Protocol p = new Protocol();
    outputLine = p.processInput(null, p);
    out.println(outputLine);

   while ((inputLine = in.readLine()) != null) {
    outputLine = p.processInput(inputLine,p);
    out.println(outputLine);
    if (outputLine.equals("Bye"))
            {
                System.out.print("Closing socket.");
                break;
            }
    }


    out.close();
    in.close();
    socket.close();
 catch (IOException e)
 {
    // .....
  }

}

public String processInput(String theInput, Protocol p) 
 {
     theOutput = null;


     frame = new JFrame ("Find The Word!");
      frame.addWindowListener( new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
              theOutput = "Bye";
        }
    });

    frame.add(p);
    frame.pack();

    frame.setVisible(true);





    return theOutput;
} 

但它不起作用,我必须手动结束服务器端的进程。我认为它在while循环中卡住了,但我无法弄清楚它是什么问题。

1 个答案:

答案 0 :(得分:0)

假设您无法诱使服务器向您发送Bye消息,您可以退出循环,就好像它已向您发送了Bye消息或从另一个线程关闭套接字。