Link to home
Start Free TrialLog in
Avatar of mzimmer74
mzimmer74

asked on

EOFException

Hey all,
I'm seeing a slightly weird error in a client/server program I'm working on.  I have a class I wrote with a send and receive method in it.  These appear to work quite well except for every once in a while I get an EOFException on my readInt() call to inputStream (which is a DataInputStream created from a socket).  I have no idea how to reproduce this regularly, and can't figure out what might be wrong with this.  Please take a look at my send and receive methods to see if I'm missing something.  Thanks.


public void send(byte[] buffer) throws SocketException, IOException
{
   int size = buffer.length;
   outputStream.writeInt(size);
   outputStream.write(buffer);
}

public byte[] receive() throws SocketException, IOException
{
  int size = inputStream.readInt();
  byte[] buffer = new byte[size];
  inputStream.readFully(buffer);
  return buffer
}

Avatar of mzimmer74
mzimmer74

ASKER

there should of course be a ";" after "return buffer" in my receive method.
ASKER CERTIFIED SOLUTION
Avatar of Venci75
Venci75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, the few times we've seen this exception it's been in the course of "normal" operation.  To the best of my knowledge my socket is alive and the connection should be good.  Now, I suppose I may not know that the socket is closed (ie was shut down by something outside of my control) but is there anything else that might cause this?
the only reason to get this could be reading -1 from the Socket stream. -1 means that the connection is closed by the remote host
You got me on the right track with this.  What was happening was every time a user logged in (even incorrectly) I created a new socket connection instead of keeping an old one.  Then, when the user closed out the good connection and shut down the "bad" connection got all confused and crashed.  Basically I just had way too many connections.  Thanks for the help.