SocketException: socket closed

◇◆丶佛笑我妖孽 提交于 2019-12-24 03:59:10

问题


I am creating a Chat application with Java Sockets, and am getting the SocketException: socket closed when closing the server. I receive the error even if I never attempt to connect clients- just stopping the server will have this socket closed exception.

I know what the error means, I'm trying to access the socket once it has already been closed, but I can't figure out why this would happen.

public ServerSocket server_socket;
new ConnectionThread(server_back_end);

class ConnectionThread extends Thread implements Runnable, Serializable
{
    public ServerBackEnd server_back_end;

    public ConnectionThread(ServerBackEnd server_back_end) /*constructor*/
    {
        this.server_back_end = server_back_end;
        start();
    }

    @Override
    public void run()
    {
        try
        {
            server_back_end.server_socket = new ServerSocket(8080);
            server_back_end.server_front_end.display("<<SERVER STARTED>>");

            while (server_back_end.running)
                add_incoming_clients();
        }
        catch (Exception e)
        {
            e.printStackTrace(); //this gets thrown when I stop server
        }
    }//end run()

I think the problem is in the add_incoming_clients(), because when I comment out the call, it does not throw the error. Even though I am not dealing with any clients, something happens at the accept() method:

    public void add_incoming_clients() throws IOException, ClassNotFoundException
    {
        Socket client_socket = server_back_end.server_socket.accept(); //line 172

        ClientInfo new_client = new ClientInfo(
                new ObjectOutputStream(client_socket.getOutputStream()),
                new ObjectInputStream(client_socket.getInputStream()));
     //...
     //...
    }

Here is the code when I stop the server::

 public void stop()
    {
        if (running)
            try
            {
                running = false;
                server_socket.close();
                server_front_end.display("<<SERVER TERMINATED>>");
            }
            catch (IOException e)
            {
                System.out.println("stop server exception, is not the problem");
            }
    }//end stop()

Can someone explain what I must do to close it properly without an exception? Here is the error (line 172 is the culprit it seems, but I don't understand why)

java.net.SocketException: socket closed
    at java.net.DualStackPlainSocketImpl.accept0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
    at java.net.ServerSocket.implAccept(ServerSocket.java:545)
    at java.net.ServerSocket.accept(ServerSocket.java:513)
    at ConnectionThread.add_incoming_clients(ServerBackEnd.java:172)
    at ConnectionThread.run(ServerBackEnd.java:159)

回答1:


This Exception is your friend. It releases the thread that's waiting on ServerSocket.accept(), and proper handling will let you clean up whatever needs to be cleaned up before you shut down completely... You need to treat it like a friend :)




回答2:


Looking at the trace and at the code, Whenever you stop(), the first thing you should do is to notify the Connection Thread for it to stop its activities - and then continue with the stop method as soon as you know the connection thread is dead.

What may happens is that the stop force closes the socket while the connection thread is using it. You can see that on the trace



来源:https://stackoverflow.com/questions/36459613/socketexception-socket-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!