java.net.SocketException:Socket已关闭

时间:2014-04-16 19:32:09

标签: java sockets

当用户连接到我的服务器时,我接受连接,将连接的客户端详细信息存储在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 Class;

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();
}

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

1 个答案:

答案 0 :(得分:2)

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