gpt4 book ai didi

java.net.SocketException : Socket is closed

转载 作者:行者123 更新时间:2023-11-30 03:56:13 25 4
gpt4 key购买 nike

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

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.net.SocketException : Socket is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23118540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com