2011-11-29 33 views
1

我正在製作一個簡單的聊天服務器,並且只是爲了讓每個連接都在新線程上運行。無法獲得服務器套接字關閉

舊版本爲服務器啓動了一個單線程,它做了一個while循環,當發送停止消息然後關閉套接字時,它將停止。

新版本永遠循環併爲每個新連接創建一個新線程。現在我無法關閉套接字連接。

如果按下某個鍵並停止主線程,則該插口保持打開狀態。因此,當我再次運行程序時,我需要更改套接字號。服務器的

代碼

while(true) 
      { 
       /////////////////////////////////////////////////// 
       // get a new connection 
       /////////////////////////////////////////////////// 
       System.out.println("Aceepting connections on port 1030 \r"); 

       try{ 
        // Get New Connection 

        // wait for ever on accepting new connections 
        server.setSoTimeout(0); 
        connection=server.accept(); 

        cConnection thread = new cConnection("thread3", connection); 
      } catch(IOException ec) 
      { 
        System.out.println(ec.getMessage());  
      } 
} 

代碼啓動服務器

現在每個郵件進入一個新的線程,所以我不能告訴它停止並關閉套接字。

回答

2

您需要提供一個必須全局訪問的標誌,因此當某些客戶端想要停止服務器時,請更改變量並停止bucle。舉例:

class YourServer { 
    private static boolean execute = true; 

    public static synchronized void stop() { 
    execute = false; 
    } 
    public void yourMethod() { 
    while(execute) { 
     // implement your server here 
    } 
    } 
} 

當客戶機發送命令阻止你必須做

YourServer.stop(); 
0

如果你想有一個stop命令停止你可以調用System.exit(服務器)來強制程序存儲或只是關閉server可能是你所需要的。

0

展望你的問題,我明白了一件事,就是因爲你是把

,而(真),那麼你的控件始終卡在連接= server.accept();傾聽新的連接。所以爲了停止套接字,你需要先找到一種方法來停止while循環中的循環。您可以設置一個變量,例如(int clientsConnected)來檢查客戶端的數量,何時停止該while循環。所以你可以停止你的套接字。

下面是我的示例代碼爲客戶端正在做關閉套接字相同的事情。 希望這可以解決您的問題。

class GetNamesFromServer implements Runnable 
    { 
     private Socket sForName, sForId; 

     private BufferedReader in, inForName, inForId; 
     private PrintWriter outForName, outForId; 

     private static String clientNames; 

     public GetNamesFromServer(Socket s1, Socket s2) 
     { 
     sForName = s1; 
     sForId = s2;  
     } 

     public void run() 
     {  
     try 
     { 
      outForName = new PrintWriter(sForName.getOutputStream(), true); 
      outForName.println(Client.clientName); 
      System.out.println("Send Name : " + Client.clientName); 
      outForName.flush(); 
     } 
     catch(IOException e) 
     { 
      System.err.println("Error sending Name to the Server."); 
     } 

     try 
     { 
      inForId = new BufferedReader(new InputStreamReader(sForId.getInputStream())); 
      Client.clientId = (inForId.readLine()).trim(); 
      System.out.println("Client ID is : " + Client.clientId); 
     } 
     catch(IOException e) 
     { 
      System.err.println("Error Receiving ID from Server."); 
     } 

     try 
     { 
      inForName = new BufferedReader(new InputStreamReader(sForName.getInputStream())); 
      while (true) 
      {   
      clientNames = inForName.readLine(); 
      if (clientNames != null && clientNames != "") 
      { 
       clientNames = clientNames.substring(1, clientNames.length() - 1); 
       System.out.println("Names Received : " + clientNames);   
       String[] names = clientNames.split(", "); 
       Client.nameClients.clear(); 
       for (String element: names) 
        Client.nameClients.add(element); 
       Client.nPane.setText(""); 
       int size = Client.nameClients.size(); 
       System.out.println("Size of list : " + size); 
       for (int i = 0; i < size; i++) 
       {   
       String name = Client.nameClients.get(i);   
       String colour = Character.toString(name.charAt(0)); 
       name = name.substring(1, name.length()) + "\n"; 
       appendToNamePane(name, ReceiveMessages.getColour(Integer.parseInt(colour)), "Lucida Console"); 
       } 
       System.out.println("Clients Online : " + Client.nameClients);   
      } 
      int index = Client.nameClients.indexOf(Client.clientId + Client.clientName); 
      **if (index == -1) 
      { 
      sForName.close(); 
      break; 
      }** 
     } 
     } 
     catch(IOException e) 
     { 
     System.err.println("Error Receiving Names of Clients from Server"); 
     } 
    } 

NEW版: 您可以添加一個帽給客戶的最大數量,可以連接,一旦到達while循環不會去connection = server.accept();,因此他們完成閒聊時(一段時間後)即totalClients = 0,您也可以停止套接字,以停止程序。

if (totalClients == 0) 
{ 
    socket.close(); 
    serverSocket.close(); 
} 

問候

相關問題