简体   繁体   English

使用新端口重新启动Java ServerSocket

[英]Restart java ServerSocket with new port

I'm trying to write a function to restart my ServerSocket with new port. 我正在尝试编写一个函数以使用新端口重新启动ServerSocket。 The new port is provided by the user through the panel (that part of code is correct) in which the restart() function is colled. 用户通过面板(该部分代码正确)提供了新端口,在该面板中对restart()函数进行了整理。 The new port is saved to the static field portNumString. 新端口将保存到静态字段portNumString。

The following is my code of the attempt to restart the server. 以下是我尝试重新启动服务器的代码。 The restarted server is not working. 重新启动的服务器无法正常工作。 The original port works correctly. 原始端口正常工作。

 public static void main(String [] args) throws IOException, ClassNotFoundException
   {         
      runServer(false);
   } 


 public static void runServer(boolean changePort)
   {        
      try
        {
           ServerSocket socket = new ServerSocket(Integer.parseInt(portNumString));
           Server server = new Server(socket);

           while(!changePort)
             {             
               server.accept();  

               if(changePort && socket!=null)
                 {
                   socket.close();                  
                   runServer(false);
                 }                    
             } 
        }
      catch(Exception e)
        {
           System.out.println("EXCEPTION !!! "+e);              
        }        
   }


 public static void restart() throws NumberFormatException, ClassNotFoundException, IOException
   {
      System.out.println("Restart Called... ");
      runServer(true);
   }

EDIT: 编辑:

The part of code that calls the restart() 调用restart()的代码部分

         cloesButton.addActionListener(new ActionListener()
                       {
                          @Override
                          public void actionPerformed(ActionEvent e)
                            {                                  
                               portNumString=txtPort.getText();                                   
                               try
                                {
                                  restart();
                                }
                              catch(NumberFormatException e1)
                                {
                                  e1.printStackTrace();
                                }
                            }
                       });

I am assuming restart() is being called from another thread, since the runServer method has a loop that never terminates (since changePort is a local parameter and nothing changes it inside the loop) 我假设从另一个线程调用restart() ,因为runServer方法具有一个永不终止的循环(因为changePort是本地参数,并且在循环内没有任何更改)

restart() is not stopping the existent server. restart()不会停止现有服务器。

It is just going through the method again starting a new ServerSocket , with the old one still running. 它只是通过该方法再次启动一个新的 ServerSocket ,而旧的仍在运行。

Since the boolean changePort is true, it won't even go inside the while loop, and never accepts a client socket. 由于布尔值changePort为true,因此它甚至不会进入while循环,并且永远不会接受客户端套接字。 On the other hand the old server socket is still happily running. 另一方面,旧的服务器套接字仍在愉快地运行。

In order to get this working properly you need to use a proper class, instead of a bunch of static methods. 为了使其正常工作,您需要使用适当的类,而不是一堆static方法。 Put things like the port number and the serverSocket as member variables. 将端口号和serverSocket作为成员变量。 Then check those in your while loop, without calling the method again. 然后在您的while循环中检查那些内容,而无需再次调用该方法。 Something like: 就像是:

while (true) {
   this.serverSocket = new ServerSocket(this.portNum);

   try {   
     Socket client = server.accept();  

      //todo: do something with the client
   } 
   catch (IOException ex) { 
      //an IO error occurred, probably we were asked to restart
   }
}

Then you can have something like: 然后,您可能会遇到类似:

void restart(int portNum) {
  this.portNum = portNum;
  try {
    this.serverSocket.close();
  } catch (IOException ex) {
    //todo: handle it or log it somewhere
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM