Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to detect physical TCP/IP desconnection

285 views
Skip to first unread message

George Wilkin

unread,
May 4, 2003, 5:11:29 PM5/4/03
to
Hello, I have a program Client receives some data from Server by TCP/IP
(with recv()). And I need to detect in client when something abnormal
happens to the server (physical ethernet cable disconnection, PC power
shutdown, etc..).
I.e., when I terminate the Server process (in task manager), there is no
problem, and client knows about this, because its recv() call fails, but I
suppose that it is because the server process' socket is closed by OS at
that moment. But when I suddenly switch off the server's PC, or switch off
the hub, or disconnect the server net cable, client's recv() doesnt fail(!)
and client continues waiting for the data to arrive. So how can this
physical failure be detected?

Thank you.


Alex Fraser

unread,
May 4, 2003, 5:54:08 PM5/4/03
to
[FU-T: alt.winsock.programming]

"George Wilkin" <g...@dontspam.com> wrote in message
news:b93vml$dh7$1...@nsnmpen2-gest.nuria.telefonica-data.net...


> Hello, I have a program Client receives some data from Server by TCP/IP
> (with recv()). And I need to detect in client when something abnormal
> happens to the server (physical ethernet cable disconnection, PC power
> shutdown, etc..).

[snip]

The only reliable method is to timeout the receive operation. To guarantee
you don't get stuck in recv(), you must use non-blocking sockets. You can
then use select() (or WSA*Select() and related functions) with the socket to
block until you should call send()/recv() or the timeout is reached.

Alex


George Wilkin

unread,
May 4, 2003, 6:34:07 PM5/4/03
to
Thank you, but my client application has a separate thread and inside it has
a recv() call, that sometimes receives data (when server sends it), but it
is not receiving continuously, and there can be an interval up to several
minutes between two sends.
So is there any way to check periodically (in client) that the server socket
is still "alive"? (that the server PC is working)?
Or another approach, is there a way to make this recv() call return error
immediately, when detecting that the server has broken?

Thank you,

Ger.

Hector Santos

unread,
May 4, 2003, 8:13:18 PM5/4/03
to
If you are recv()ing and a socket disconnect occurs, it should return a ZERO
or SOCKET_ERROR value. From the docs:

Return Values:

If no error occurs, recv returns the number of bytes received. If the
connection has been gracefully closed, the return value is zero. Otherwise,
a value of SOCKET_ERROR is returned, and a specific error code can be
retrieved by calling WSAGetLastError.

You can also use the select() command to detect when data is available
before calling recv(). select() will return a SOCKET_ERROR if the socket is
closed.

---


"George Wilkin" <g...@dontspam.com> wrote in message

news:b944hi$5i4$1...@nsnmpen2-gest.nuria.telefonica-data.net...

Alex Fraser

unread,
May 4, 2003, 8:58:28 PM5/4/03
to
"George Wilkin" <g...@dontspam.com> wrote in message
news:b944hi$5i4$1...@nsnmpen2-gest.nuria.telefonica-data.net...

> Thank you, but my client application has a separate thread and inside it
> has a recv() call, that sometimes receives data (when server sends it),
> but it is not receiving continuously, and there can be an interval up to
> several minutes between two sends.
> So is there any way to check periodically (in client) that the server
> socket is still "alive"? (that the server PC is working)?

Yes, periodically send something; if you get a reply then you can be sure
the connection and server were working for some period between the time you
sent and the time you received. More useful than it sounds :).

> Or another approach, is there a way to make this recv() call return error
> immediately, when detecting that the server has broken?

No. If you're only receiving, there may never be notification (of ANY sort,
not just notification by Winsock to your application) that anything has
happened. Instead of detecting the presence of failure, you must presume
it - detect the absence of success with a timeout.

Alex


Alex Fraser

unread,
May 4, 2003, 9:13:58 PM5/4/03
to
A few additional points...

"Alex Fraser" <m...@privacy.net> wrote in message
news:b94cvk$fe33d$1...@ID-149533.news.dfncis.de...


> "George Wilkin" <g...@dontspam.com> wrote in message
> news:b944hi$5i4$1...@nsnmpen2-gest.nuria.telefonica-data.net...

[snip]


> > So is there any way to check periodically (in client) that the server
> > socket is still "alive"? (that the server PC is working)?
>

> Yes, periodically send something [...]

As an example, have a look at what IRC servers do to detect defunct clients.

> > Or another approach, is there a way to make this recv() call return
> > error immediately, when detecting that the server has broken?
>
> No. If you're only receiving, there may never be notification (of ANY
> sort, not just notification by Winsock to your application) that anything

> has happened. [...]

Note that if you are sending, but the data is not acknowledged soon enough
by the remote host (server in this case), the connection will detectably
fail. This is not a guarantee that the server has received the data, only
the server can tell you that. (There is of course the unavoidable problem
that the server might have received it, but you never receive the
acknowledgement.)

Alex


Przemek Wasylko

unread,
May 5, 2003, 3:59:37 AM5/5/03
to
"George Wilkin" <g...@dontspam.com> wrote in message
news:b93vml$dh7$1...@nsnmpen2-gest.nuria.telefonica-data.net...

> Hello, I have a program Client receives some data from Server by TCP/IP
> (with recv()). And I need to detect in client when something abnormal
> happens to the server (physical ethernet cable disconnection, PC power
> shutdown, etc..).

I would suggest:

1. Use recv() / send() with programmed timeouts
2. getsockopt() with SO_CONNECT_TIME parameter. When connection is dead, it
will return 0.


Przemek Wasylko

unread,
May 5, 2003, 4:00:39 AM5/5/03
to
> "George Wilkin" <g...@dontspam.com> wrote in message

> news:b93vml$dh7$1...@nsnmpen2-gest.nuria.telefonica-data.net...

> > Hello, I have a program Client receives some data from Server by

> > TCP/IP (with recv()). And I need to detect in client when something

> > abnormal happens to the server (physical ethernet cable

> > disconnection, PC power shutdown, etc..).

>

I would suggest:

arkadyf

unread,
May 5, 2003, 7:04:35 AM5/5/03
to
That nice for start , but George ask about disconnect of connection
established and that
possible only through some keep-alive scenario as advised , additionally ,
disconnection
on this computer possible to check with some of the methods described on
http://www.ndis.com/faq/QA01050301
Arkady

"Przemek Wasylko" <was...@removeme.clubbing.pl> wrote in message
news:#9l0dwtE...@TK2MSFTNGP12.phx.gbl...

David Schwartz

unread,
May 5, 2003, 8:41:28 AM5/5/03
to

"George Wilkin" <g...@dontspam.com> wrote in message
news:b944hi$5i4$1...@nsnmpen2-gest.nuria.telefonica-data.net...

> Thank you, but my client application has a separate thread and inside it
has
> a recv() call, that sometimes receives data (when server sends it), but it
> is not receiving continuously, and there can be an interval up to several
> minutes between two sends.
> So is there any way to check periodically (in client) that the server
socket
> is still "alive"? (that the server PC is working)?
> Or another approach, is there a way to make this recv() call return error
> immediately, when detecting that the server has broken?

If you could detect an error if the connection was broken for only a
split second, every network hiccup would break your connection. TCP is
specifically designed to be resilient to network outages.

DS


John Smith

unread,
May 5, 2003, 11:11:11 AM5/5/03
to
Right; either TCP level keep alives or define some sort of NO-OP that the
client can send to the server harmlessly. If the computer hosting the
service is alive, and the service is alive, an ACK will be sent, and
everything will be fine at the TCP layer. However, if the computer is
non-reachable at that time, for whatever reason (cut wire/crashed
server/etc), the client side will eventually (in short order) give up the
ghost on the connection, and your recv should then fail.

--
Tom

"arkadyf" <ark...@hotmail.com> wrote in message
news:%23Lpil2u...@TK2MSFTNGP12.phx.gbl...

George Wilkin

unread,
May 5, 2003, 12:55:24 PM5/5/03
to
I activate SO_KEEPALIVE option in both client and server sockets with

int i = 1;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (const char*) &i, sizeof i))

but nothing changes, i.e., when I disconnect net cable in server, client's
RECV() calls never
returns and keeps waiting for data(???).

Thank you.


Phil Frisbie, Jr.

unread,
May 5, 2003, 2:04:37 PM5/5/03
to

The socket option SO_KEEPALIVE was depreciated even as it was originally added
to the TCP spec! The default time-out value is two hours before sending the
keepalive message. And the time-out is global, so changing it affects all
applications on your computer.

If you need to quickly know when the connection is broken then you should add
this to your protocol. Simply sending your own own keepalive message when no
messages have been received will do what you want. That way you can determine
the time-out and even alter it on the fly.

> Thank you.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

Tom Stewart

unread,
May 5, 2003, 3:27:45 PM5/5/03
to
Define never.

There is a registry value that governs this timeout, but as Phil says, it is
global, and it may not work on all platforms going back (like 95). That's
why we did our own.

--
Tom

"Phil Frisbie, Jr." <ph...@hawksoft.com> wrote in message
news:3EB6A7B4...@hawksoft.com...

arkadyf

unread,
May 6, 2003, 1:46:30 AM5/6/03
to
Przemek !
But SO_CONNECT_TIME doesn't exist in winsock maybe that's the feature of
Unix Oses ?
Arkady

"Przemek Wasylko" <was...@removeme.clubbing.pl> wrote in message
news:#9l0dwtE...@TK2MSFTNGP12.phx.gbl...

arkadyf

unread,
May 6, 2003, 1:52:47 AM5/6/03
to
That one is KeepAliveTime under
HKLM\System\CurrentControlSet\Services\Tcpip\Parameters
Arkady

"Tom Stewart" <tast...@msdn.microsoft.com> wrote in message
news:O$o8sxzED...@TK2MSFTNGP12.phx.gbl...

George Pavlenko

unread,
May 6, 2003, 8:31:51 AM5/6/03
to
"arkadyf" <ark...@hotmail.com> wrote in message
news:ex3Oep4E...@TK2MSFTNGP12.phx.gbl...

> Przemek !
> But SO_CONNECT_TIME doesn't exist in winsock maybe that's the feature of
> Unix Oses ?

I use

1) select
2) recv

(in this order)

if socket is ready for reading or dropped "select" terminates it's
waiting and then I call "recv" and if socket is on-line the function
reads bytes, another way - it returns error...

arkadyf

unread,
May 6, 2003, 9:54:44 AM5/6/03
to
That's OK , but my problem , that I cannot find SO_CONNECT_TIME
Arkady

"George Pavlenko" <em...@teleport.kiev.ua> wrote in message
news:b989vo$6jl$1...@news.dg.net.ua...

SenderX

unread,
May 6, 2003, 1:34:58 PM5/6/03
to
"arkadyf" <ark...@hotmail.com> wrote in message
news:OxN3R68E...@TK2MSFTNGP10.phx.gbl...

> That's OK , but my problem , that I cannot find SO_CONNECT_TIME
> Arkady

#include <MSWSock.h>


arkadyf

unread,
May 7, 2003, 3:55:31 AM5/7/03
to
Thanks
Arkady

SenderX <x...@xxx.com> wrote in message
news:6nSta.785212$S_4.808157@rwcrnsc53...

0 new messages