Java: Prevent Socket from throwing EOFException?

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.

Up vote 0 down vote favorite share g+ share fb share tw.

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.

Java link|improve this question asked Jan 18 '11 at 21:30user4894812113 0% accept rate.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions