Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   detecting Socket disconnect (http://www.velocityreviews.com/forums/t953517-detecting-socket-disconnect.html)

bob smith 10-17-2012 08:12 PM

detecting Socket disconnect
 
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?

Arne Vajhoej 10-17-2012 08:45 PM

Re: detecting Socket disconnect
 
On 10/17/2012 4:12 PM, bob smith wrote:
> 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



Lew 10-17-2012 08:47 PM

Re: detecting Socket disconnect
 
bob smith wrote:
> 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...l#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...html#isClosed()
tell you?

--
Lew

Arne Vajhoej 10-17-2012 09:19 PM

Re: detecting Socket disconnect
 
On 10/17/2012 4:47 PM, Lew wrote:
> bob smith wrote:
>> 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?


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.

>> Anyone know how to detect the disconnect?

>
> http://docs.oracle.com/javase/7/docs...l#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



Arne Vajhoej 10-17-2012 09:24 PM

Re: detecting Socket disconnect
 
On 10/17/2012 4:59 PM, Peter Duniho wrote:
> On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith wrote:
>
>> 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?

>
> 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



Roedy Green 10-17-2012 10:52 PM

Re: detecting Socket disconnect
 
On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith
<bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>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.



Steven Simpson 10-18-2012 08:31 AM

Re: detecting Socket disconnect
 
On 17/10/12 21:59, Peter Duniho wrote:
> On Wed, 17 Oct 2012 13:12:13 -0700 (PDT), bob smith wrote:
>> I have a java.net.Socket that I only write to, and I can't figure out how to tell if it's disconnected.

> 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 is the case in C, where functions like recv() use -1 to signal an
error and 0 for EOF. However, in Java, InputStream (such as you get
from a Socket) returns -1 to signal EOF.

<http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html>


--
ss at comp dot lancs dot ac dot uk



All times are GMT. The time now is 03:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.