当用户连接到我的服务器时,我接受连接,将连接的客户端详细信息存储在arraylist中,然后尝试将列表发送给客户端以创建异步列表。但是,当我尝试发送列表时,它会断开连接并导致错误。

java.net.SocketException: Socket is closed

我的服务器类;
        while (true) {
            Socket clientSocket = serverSocket.accept();
            ServerClientComs clientThread = new ServerClientComs(clientSocket);
            getUsername(clientSocket, clientThread);
            updateList();
            try {
                clientThread.sendList(connections);
                clientThread.initialiseCommunication();
                clientThread.start();
            }
            catch (IOException error) {
                System.out.println("Server: Unable to initialise client thread! - " + error);

            }
        }

ServerClientComs类;
public ServerClientComs(Socket clientSocket) {
    super();
    this.client = client;
    this.clientSocket = clientSocket;
}

public void initialiseCommunication() throws IOException {
    output = new PrintWriter(this.clientSocket.getOutputStream(), true);
    input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}

public void sendList(ArrayList connections) throws IOException
{
    oos = new ObjectOutputStream(this.clientSocket.getOutputStream());
    oos.writeObject(connections);
    oos.close();
}

已经玩了一个小时,现在变得烦人了。任何帮助是极大的赞赏!

最佳答案

“关闭套接字”异常意味着捕获到该异常的应用程序关闭了该套接字,然后继续尝试使用它。请注意,关闭套接字的输入或输出流会同时关闭另一个流和套接字。根据您的陈述,您似乎在使用套接字发送数据之前关闭了流。

关于java - java.net.SocketException : Socket is closed,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23118540/

10-10 07:51