没有在Java中检测客户端的远程关闭

时间:2016-11-02 13:53:55

标签: java multithreading networking

我有以下代码,我从我的主要调用如下...

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 个答案:

答案 0 :(得分:0)

循环没有任何条件 - 无限

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

你必须对套接字,读/写/选择做一些事情,以检测它实际上是由远程对等体关闭的。例如(糟糕的用法,例如)

while (socket.getInputStream().read() >= 0)

答案 1 :(得分:0)

原来检查客户端的方法是检查readLine()是否为null。

while (true)
                {
                    try
                    {
                        String line = in.readLine();
                        if (line == null)
                            return;
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }

答案 2 :(得分:0)

在TCPMultiServer中,您将不断创建新的处理程序

HELLO

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