detecting Socket disconnect

B

bob smith

I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

Anyone know how to detect the disconnect?
 
A

Arne Vajhoej

I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

Anyone know how to detect the disconnect?

I believe the most reliable method is to implement a protocol that
contains frequent keep alive messages and use missing such to detect
a network failure. Otherwise you can try send and wait for timeout.

Arne
 
L

Lew

bob said:
I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

I call socket.isConnected(), but it still returns true after I broke the TCP connection. I had telnetted in thru Windows telnet and closed the window.

Closed what window?

How do you know that closing "the window" broke the connection?

What reads from the other end of the connection?
Anyone know how to detect the disconnect?

http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected()
"Returns: true if the socket was successfuly connected to a server"

Your socket was connected, so according to the docs you should get 'true'.

What does
http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isClosed()
tell you?
 
A

Arne Vajhoej

Closed what window?

The window of the program he mentioned (Windows telnet) seems
a good guess.

The window of his email program would not make much sense.
How do you know that closing "the window" broke the connection?

Broke is a bit fuzzy term.

But obviously a closed program (Windows telnet terminated when the
window is closed) can not receive from the connection.
What reads from the other end of the connection?

Whatever uses a java.net.Socket - I would expect a Java program.
http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected()
"Returns: true if the socket was successfuly connected to a server"

Your socket was connected, so according to the docs you should get 'true'.

Unfortunately it will only tell status in the apps own end.

Arne
 
A

Arne Vajhoej

Not enough information in your question. What kind of "disconnected" are
you asking about?

He explicit said that the program in the other end was terminated.
The normal way to detect a disconnect is to read from the socket and get a
return value of 0 bytes for the count of bytes. This tells you that the
remote end is done sending (even if it never sent anything) and is ready to
close the connection.

Is that fail fast and reliable?
If the connection was reset somehow, such as because of a network failure,
then you should get an error (exception) when you attempt to send. There's
usually really no point in trying to detect the failure _before_ you would
normally send, because the failure could be something in between the end
points that recovers. Explicitly attempting to detect the failure can and
will create failures that otherwise would not have been a problem.

That is typical what is done.

Even though I suspect that it may not always be fast.

Arne
 
R

Roedy Green

Anyone know how to detect the disconnect?

I wrestled with the problem and solved it with a custom protocol
sending packets back and forth.

If there is no traffic, it sends a "heartbeat" packet every X seconds,
just to let the other end know it is alive. The receiver just ignores
them. If a read times out, it presumes the worst and restarts the
socket.

TCP/IP is quite happy with no traffic for long periods of time, so it
is up to you if you want early notification.
--
Roedy Green Canadian Mind Products http://mindprod.com
There are four possible ways to poke a card into a slot.
Nearly always, only one way works. To me that betrays a
Fascist mentality, demanding customers conform to some
arbitrary rule, and hassling them to discover the magic
orientation. The polite way to do it is to design the reader
slot so that all four ways work, or so that all the customer
has to do is put the card in the vicinity of the reader.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top