Socket Exception: socket is closed [duplicate]

吃可爱长大的小学妹 提交于 2019-12-23 13:08:40

问题


I want to create server which is able to be connected with multiple clients. My main function is:

ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(5556);
    } catch (IOException ex) {
        Logger.getLogger(MakaoServer.class.getName()).log(Level.SEVERE, null, ex);
    }
    while (true) {
        try {
            Socket connection = serverSocket.accept();
            PlayerConnection playerConn = new PlayerConnection(connection);
            playerConn.start();
        } catch (IOException ex) {
            System.out.println("Nie można było utworzyć gniazda.");
        }
    }

PlayerConnection is a Thread class. The run method:

public void run() {
    InputStream input = null;
    while (true) {
        try {
            input = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String msg = reader.readLine();
            System.out.println(msg);

        } catch (IOException ex) {
            Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

        }
    }
}

When I run client and send message to server it's received but on the next while loop iteration connection.getInputStream(); throws an Socket Exception socket is closed. Why?

java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at makaoserver.PlayerConnection.run(PlayerConnection.java:38)

回答1:


Put the input stream and buffered reader outside the loop.

Possibly creating multiple stream connected to the same input stream is causing this.




回答2:


try

public void run() {
    InputStream input = null;
    input = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));

    while (true) {
        try {
            String msg = reader.readLine();
            System.out.println(msg);
        } catch (IOException ex) {
            Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

        }
    }
}

Also don't forget to close sockets when your done with them

try {
  // Create objects
  // do stuff
} finally {
  if (obj != null) obj.close();
}


来源:https://stackoverflow.com/questions/8263141/socket-exception-socket-is-closed

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