I have code that is similar to the code below. The problem that I'm having is that when the ObjectInputStream (the input variable) tries to read an object (which will only happen after the connection has been established), it crashes and gives me an error:
java.net.SocketException: Connection reset
and refers to the line with:
message = (String) input.readObject();
It goes into the ex exception loop when it should only go there if the client disconnects (As far are I know).
@Override
public void run() {
String message = "";
do{
try{
message = (String) input.readObject();
}catch(ClassNotFoundException cnfException){
System.err.println("Unable to read client data.");
}catch(Exception ex){ // Will get called if the client is no longer in communication with the server.
continueTalking = false; // If the client disconnects, the server will shut down its communication with the client.
ex.printStackTrace();
break; // Breaks out of the do...while loop.
}
if(message.equals("DISCONNECT")){
continueTalking = false;
System.out.println(connection.getLocalAddress() + " disconnected from the session.");
}
else{
//TODO Send the message off to be processed.
}
}while(continueTalking); // Continues waiting for a message until continueTalking = false
closeStreams();
}
Thank you!
UPDATE: I figured it out. I originally tried out EOFException, but for some reason that was never called. I eventually found out that it was a problem with my client side that it wasn't actually sending any data and disconnected itself as soon as it ran. For anyone experiencing the same problem as me, here is my fixed code:
/** This should handle the connection **/
@Override
public void run() {
/** The message that the client has sent to the server. **/
String message = "";
do{
try{
message = (String) input.readObject(); // Waits for the client to send a message to the server.
}catch(ClassNotFoundException cnfException){
System.err.println("Unable to read client data. Continuing on with my life.");
}catch(Exception noConnectionToSocketFound){ // Will get called if the client is no longer in communication with the server.
System.out.printf("User with handle: %s disconnected.\n", clientIP);
continueTalking = false; // If the client disconnects, the server will shut down its communication with the client.
break; // Breaks out of the do...while loop that is constantly waiting for the client to send a message to.
}
/** If the client sends the message "DISCONNECT", then the server will shut down all communications with said client. **/
if(message.equals("DISCONNECT")){
System.out.println(connection.getLocalAddress() + " disconnected from the session properly.");
continueTalking = false; // If the client wants to disconnect, then the server will stop trying to communication with said client.
break; // Breaks out of the do...while loop that is constantly waiting for the client to send a message to.
}
else if(message != ""){
System.out.printf("Got message from %s:\n%s\n", clientIP, message);
message = ""; // Is needed so that this loop doesn't get called every time after the first message has been sent. Without it, after the word "cheeseburger" is sent, it would continually think that "cheeseburger" is being repeatedly sent (this might not actually happen anymore).
//TODO Send the message off to be processed.
}
}while(continueTalking); // Continues waiting for a message until continueTalking = false
closeStreams(); // Just some clean up
The
continueTalking = false;
break;
might be a bit redundant and unnecessary (I would only really need one of them), but I feel better knowing that there are two things to fall back on.
Hope this helps!