2013-05-17 110 views
0

我有这个多服务器线程,所以无论何时'X'按钮点击右上角,我想在服务器端显示消息“关闭套接字”。Java网络服务器,协议和客户端

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循环,但我无法弄清楚它有什么问题。

+0

按钮的监听器中有什么? –

+0

你不能添加一个监听器到窗口按钮。它使用WindowAdapter正确处理。 – desperateCoder

+0

这样它会返回“再见”,为了打破服务器内部的while循环? – june

回答

0

假设你不能导致服务器发送一个BYE消息,您可以退出循环,如果它已经给你发了再见消息或从另一个线程关闭套接字。