1

My client/server application currently keeps opening and closing new connections every time it wants to send/receive data. I'm trying to change it so it will have one persistent connection.

The problem I'm having is the socket's DataInputStream on the server keeps throwing EOFException's when I just want it to block until it receives the next batch of data.

I thought about just simply writing the server like this...

while socket is open {
    while at socket's DataInputStream's EOF {
        wait a second
    }
    //If we're here, then we have some data
    do stuff
}

... but this is extremely ugly and not the proper way to block until some data is received.

Is there a cleaner way to tell the socket to block until there's some data to read? I've tried read() and readFully(), but neither work.

user207421
  • 305,947
  • 44
  • 307
  • 483
user489481
  • 321
  • 1
  • 4
  • 13
  • 1
    A socket does block until there's data to read (unless you somehow got that socket from a non blocking SocketChannel). If you're getting an EOF exception it's because the remote end closed the socket, so you need to figure out why it does that. – nos Jan 18 '11 at 21:37
  • That was indeed the problem. Thanks for the quick replies! – user489481 Jan 18 '11 at 21:47

1 Answers1

2

If you are getting EOFException, it means the connection is gone. You cannot wait on a connection that's closed. Keep working on your client code so that it doesn't close the connection. On the server side, any of the read methods will block until data is available without further effort from you.

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61