I'm creating a multithreaded chat server that supposed to create a separate thread for each connected client. Every time a client connects, my server creates a new instance of a client handler class, that is supposed to keep track for ingoing and outgoing messages from/to that specific client.
The first time a client connects to my echo server, the server will respond with an echo of the clients response. But if I try to send a message to the server a second time, the client creates an IOException. I have the created the client application myself, but I know it works because I can communicate with other servers just fine. I'm pretty sure the problem is somewhere in the run method of this client handler class, but I can't figure out why it's not working. Here's the run method in my client handler class:
public void run() {
try (
BufferedReader in =
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream());
) {
long time = System.currentTimeMillis();
out.println("Server - " + time + ": " + in.readLine());
out.close();
try {
in.close();
} catch (IOException e) {
System.err.println("Couldn't close input stream");
}
} catch(IOException e) {
System.err.println("Got an IOException error while reading or writing from/to client");
}
}
I've guessed that I'm supposed to have some kind of while loop somewhere, but all of my attempts to implement this have failed. E.g. I've tried to change this code:
long time = System.currentTimeMillis();
out.println("Server - " + time + ": " + in.readLine());
To this:
String inputLine;
while((inputLine = in.readLine()) != null) {
long time = System.currentTimeMillis();
out.println("Server - " + time + ": " + inputLine);
}
This solution is more or less a copy of how the oracle site (http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html) says it's supposed to be done.
I think the main problem might be me not really grasping the whole concept of server/client communication, so a push in the right direction would be greatly appreciated.
Thanks in advance!