2013-05-17 26 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消息,您可以退出循環,如果它已經給你發了再見消息或從另一個線程關閉套接字。