How to check if a server has disconnected a socket

J

Jarle Jacobsen

Hi.

Can anybody help me with the following?

I'm using java.net.Socket to connect to a server. The socket is part of a
client application, and I connect to the server at startup. The application
should be able to handle a lost connection due to a restart of the server or
any other reason. Is it a way to check if the socket connection is still
connected or if the connection has been reset?



Jarle
 
G

Gordon Beaton

I'm using java.net.Socket to connect to a server. The socket is part
of a client application, and I connect to the server at startup. The
application should be able to handle a lost connection due to a
restart of the server or any other reason. Is it a way to check if
the socket connection is still connected or if the connection has
been reset?

Yes.

If reading from or writing to the streams associated with the Socket
indicates EOF, the connection has been broken and you can (attempt to)
reconnect.

Or, if you are using a java.nio.channels.Selector, the channel will
become ready for reading or writing when the connection breaks. Note
that you still need to actually read or write to determine whether EOF
has been reached.

/gordon
 
M

MERIGON Olivier

Hi,
The only way to know if the connection is alive is to write on the
socket. However depending on your protocol sometimes you're not able to
write on the socket to check the connection (for example if you are waiting
for a response, you will never be aware of this connection loose), this is
why you can set the KEEP_ALIVE option that will check the connection every 2
hours. From my experience, the methods "boolean isBound()" and "boolean
isConnected()" does not give an accurate information if you have connected
the socket once (it seems to be normal since with tcp you can't be aware of
a connection loose unless you are in the two case I was speaking about
before).
This thread discuss of this issue:
http://forum.java.sun.com/thread.jsp?forum=11&thread=539297
Hope this will help,

Olivier
 
J

Jarle Jacobsen

MERIGON Olivier said:
Hi,
The only way to know if the connection is alive is to write on the
socket. However depending on your protocol sometimes you're not able to
write on the socket to check the connection (for example if you are waiting
for a response, you will never be aware of this connection loose), this is
why you can set the KEEP_ALIVE option that will check the connection every 2
hours. From my experience, the methods "boolean isBound()" and "boolean
isConnected()" does not give an accurate information if you have connected
the socket once (it seems to be normal since with tcp you can't be aware of
a connection loose unless you are in the two case I was speaking about
before).
This thread discuss of this issue:
http://forum.java.sun.com/thread.jsp?forum=11&thread=539297
Hope this will help,

Olivier

Thanks!
The postings in that thread explained a lot.

Jarle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top