[icq-devel] Newcomer



Remy Lebeau gambit47 at yahoo.com
Sat Nov 30 23:09:54 CET 2002


--- "Christophe Minguet (freesurf)"
<c.minguet at freesurf.fr> wrote:

> His only way to know he is disconnected seems
> to be to write on the socket ...(KEEPALIVE)

Although the KEEPALIVE is part of the procotol itself
for convenience, that is not the only way, let along
eht fastest way, to detect a disconnect.  It's merely
a matter of how the socket itself is being programmed
to begin with.

If the socket is being used in asynchronous mode, the
code managing the socket is responsible for calling
the select() function in some kind of loop or timer
(this is assuming WinSock/Berkeley socket programming,
that is).  If the socket is disconnected, whether
gracefully or prematurely, select() returns that the
socket is readible again, and a subsequent call to
recv() indicates why the socket disconnected (0 for
graceful, or an error code on failure).

If the socket is being used in synchronous mode, it's
still the responsibility of the code managing the
socket to continuously read the socket for new
incoming data, in which case a disconnect, whether
gracefully or prremature, can still be detected
relatively quickly.

> I don't remember in C ... But in java you can't
> know a socket is closed on the other side when u
> try to read it, only way is to write in it.

Sure, you can.  Reading and writing are done via the
InputStream and OutputStream classes.  If InputStream
can't read the socket anymore because it disconnected,
reading from the stream throws an IOException.  So
does InputStream.available().  Particularly in newer
versions of the JDK.

There may or may not be a small delay before Java
actually detects the disconnect, but it does happen
eventually.  Because Java sockets are blocking, and
the delays would cause the code to block for awhile,
you could also try using a separate timer to detect
when the reading/writing is taking too long, and then
close your side of the connection if needed.


Gambit


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the icq-devel mailing list