2011-11-29 34 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(); 
} 

问候

相关问题