【发布时间】:2014-04-16 19:32:08
【问题描述】:
当用户连接到我的服务器时,我接受连接,将连接的客户端详细信息存储在数组列表中,然后我尝试将该列表发送到客户端以创建异步列表。但是,当我尝试发送列表时,它会中断连接并导致错误;
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();
}
玩了一个小时,现在很烦人。任何帮助是极大的赞赏!
【问题讨论】: