Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

java - SocketException: socket closed

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...