views:

14407

answers:

9

I am getting the following error trying to read from a socket. I'm doing a readInt() on that InputStream, and I am getting this error. Perusing the documentation this suggests that the client part of the connection closed the connection. In this scenario, I am the server.

I have access to the client log files and it is not closing the connection, and in fact its log files suggest I am closing the connection. So does anybody have an idea why this is happening? What else to check for? Does this arise when there are local resources that are perhaps reaching thresholds?

Many thanks, Darryl

+1  A: 

Whenever I have had odd issues like this, I usually sit down with a tool like WireShark and look at the raw data being passed back and forth. You might be surprised where things are being disconnected, and you are only being notified when you try and read.

Geoffrey Chetwood
+1  A: 
erickson
A: 

I do note that I have the following line:

socket.setSoTimeout(10000);

just prior to the readInt(). There is a reason for this (long story), but just curious, are there circumstances under which this might lead to the indicated error? I have the server running in my IDE, and I happened to leave my IDE stuck on a breakpoint, and I then noticed the exact same errors begin appearing in my own logs in my IDE.

Anyway, just mentioning it, hopefully not a red herring. :-(

A: 

I once had exactly the same scenario (client and server on the same machine) and problem, in my case it was my personal firewall. I was using openSuSE 11.0 and by default the firewall only opens a small subset of common ports. I had to allow traffic through my port first before i could use it else it was just dropped, and all my application saw was 'Connection reset'.

n002213f
A: 

I just solved this problem, In my own case the code that generates the error was:

public void run(){
 System.out.println("Server Thread " + ID + "running.");
 while(true){
  try{
   System.out.println(streamIn.readUTF());
  }
  catch(IOException e){System.out.println("Error detected");}
 }
}

the code above is a snippet from the thread. When the clients connect it works fine, but if the client disconnects exception is thrown, then the server displays a loop of "Error detected on the command line.

It surprisingly worked fine when explicitly catching that exception and leaving the block blank, that is:

public void run(){
 System.out.println("Server Thread " + ID + "running.");
 while(true){
  try{
   System.out.println(streamIn.readUTF());
  }
  catch(IOException e){}
 }
}

//hope it helps you guys :-) happy javaing

Shouldn't you break out of the while loop when you encounter an exception? Otherwise the thread continues to run forever (or at least until an exception other than `IOException` is thrown). That's why your previous code kept printing to the console in an infinite loop. Your current solution is just hiding the problem.
Mike Spross
A: 
eitama
A: 

or maybe it's more readable when you do it like this:

try {
  while(true) {
    ...
  }
catch (IOException e) {
  System.out.println("Error detected");
}
luk
A: 

This Exception occurs when socket connection is established, and server is closed suddenly, and client is still running and it will try to reset the connection.

Divya
A: 

i get the exact same thing as Divya, strange thing is that it was working a few houres ago but now it fails 4 times (still getting to the server) and gives up.

any 1 got any idears? could firewall of changed in that time by its self?

ben