Reputation: 788
When a user connects to my server, I'm accepting the connection, storing the connected clients details in an arraylist then im trying to send that list to the client to create an asynchronous list. However, when I try to send the list it breaks the connection and causes the error;
java.net.SocketException: Socket is closed
My server class;
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();
}
Been playing about with this for a good hour, getting annoying now. any help is greatly appreciated!
Upvotes: 2
Views: 11248
Reputation: 7457
A 'Socket closed' exception means that the application that caught the exception closed the socket and then kept trying to use it. Please note that closing the input or output stream of a socket closes the other stream and the socket as well. According to your statements seems you close the stream before using the socket to send data.
Upvotes: 3