代码之家  ›  专栏  ›  技术社区  ›  Lee Armstrong

在Java中未检测到客户端的远程关闭

  •  0
  • Lee Armstrong  · 技术社区  · 9 年前

    我有下面的代码,我从我的主调用如下。。。

    Thread tcpMultiServerThread = new Thread(new TCPMultiServer());
    tcpMultiServerThread.start();
    

    课程如下:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.HashSet;
    
    
    public class TCPMultiServer implements Runnable
    {
        /**
         * The port that the server listens on.
         */
        private static final int PORT = 6788;
    
        /**
         * The set of all the print writers for all the clients.  This
         * set is kept so we can easily broadcast messages.
         */
        public static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
    
        /**
         * The appplication main method, which just listens on a port and
         * spawns handler threads.
         */
        public void run() {
    
            System.out.println("The TCP multi server is running.");
            ServerSocket listener = null;
            try
            {
                listener = new ServerSocket(PORT);
                while (true)
                {
                    new Handler(listener.accept()).start();
                }
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            finally
            {
                try
                {
                    listener.close();
                    System.out.println("Client Disconnected");
                }
                catch (IOException e)
                {
    
                }
            }
    
        }
    
        private static class Handler extends Thread {
            private Socket socket;
            private PrintWriter out;
    
            public  Handler(Socket socket) {
                this.socket = socket;
            }
    
            public void run()
            {
                try {
                    // Create character streams for the socket.
                    out = new PrintWriter(socket.getOutputStream(), true);
    
                    System.out.println("Client Connected");
                    writers.add(out);
    
                    while (true)
                    {
                        try
                        {
                            long millis = System.currentTimeMillis();
                            Thread.sleep(1000 - millis % 1000);
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                } catch (IOException e)
                {
                    System.out.println(e);
                }
                finally
                {
                    if (out != null) {
                        writers.remove(out);
                    }
                    try
                    {
                        System.out.println("Client Disconnected");
                        socket.close();
                    } catch (IOException e) {
    
                    }
                }
            }
        }
    
    
    }
    

    这接受新连接,但从未检测到远程关闭。因此,我的HashSet写入程序不会减少,只会增加!我的闭包代码似乎没有一个被捕获,连接永远保持!

    3 回复  |  直到 9 年前
        1
  •  0
  •   Lee Armstrong    9 年前

    while (true)
                    {
                        try
                        {
                            String line = in.readLine();
                            if (line == null)
                                return;
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
    
        2
  •  0
  •   rustot    9 年前

               while (true)
                {
                    try
                    {
                        long millis = System.currentTimeMillis();
                        Thread.sleep(1000 - millis % 1000);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
    

    您必须对套接字(读/写/选择)执行一些操作,以检测它实际上被远程对等方关闭。例如(使用不当,仅作为示例)

    while (socket.getInputStream().read() >= 0)
    
        3
  •  0
  •   Massimo Petrus    9 年前

    在TCPMultiServer中,您将继续创建新的处理程序

      while (true)
      {
           new Handler(listener.accept()).start();
      }
    

    因此,您不断地添加和添加客户端。也许你应该做这个