Click to See Complete Forum and Search --> : TCP peer disconnection


shayw
May 30th, 2004, 03:51 PM
Hi,

I found out that although I'm using a TCP socket (which is connection-oriented), when I disconnect the network cable of the peer machine (not the one that my program is running on), my application does not recognize this. Furthermore, when I try to send information on the socket, winsock retruns with success.

Is there a way to detect at real-time (or very close to real-time) that the peer machine is disconnected from the network?

P.S I'm using Win2K OS using winsock/winsock2.

Thanks,
Shay

Bios1
May 30th, 2004, 04:39 PM
It is shown at task bar (connected or disconnected). Additional applications can show even more details.

Mathew Joy
May 31st, 2004, 01:35 AM
Originally posted by shayw
Hi,

I found out that although I'm using a TCP socket (which is connection-oriented), when I disconnect the network cable of the peer machine (not the one that my program is running on), my application does not recognize this. Furthermore, when I try to send information on the socket, winsock retruns with success.

Is there a way to detect at real-time (or very close to real-time) that the peer machine is disconnected from the network?

P.S I'm using Win2K OS using winsock/winsock2.

Thanks,
Shay Cable pullouts are usually tollerated for an amount of time by TCP in the expectation that someone will detect and plug it back so the send/recv can be resumed as normal.

There is infact a keep alive message in TCP but as explained in RFC 1122 this should be no less than 2hrs. 2hrs is the default implementation but this can be controlled by the registry parameters. However changing this value changes the behavior of all the TCP applications on the machine. If you aplication is restricted to W2k you have a ioctl command SIO_KEEPALIVE_VALS to configure on a per socket basis. If you are targeting on platforms such as 95 or 98 you may have to implement your own keepalive scheme.

shayw
May 31st, 2004, 11:50 AM
Hi Mathew,

Thanks for your reply.
Do you have any code snippet that demonstrate the usage of this SIO_KEEPALIVE_VALS mechanism?

Thanks a lot,

Shay

Mathew Joy
June 1st, 2004, 12:52 AM
DWORD dwError = 0L ;
tcp_keepalive sKA_Settings = {0}, sReturned = {0} ;

sKA_Settings.onoff = 1 ;
sKA_Settings.keepalivetime = 5500 ; // Keep Alive in 5.5 sec.
sKA_Settings.keepaliveinterval = 3000 ; // Resend if No-Reply
if (WSAIoctl(skNewConnection, SIO_KEEPALIVE_VALS, &sKA_Settings,
sizeof(sKA_Settings), &sReturned, sizeof(sReturned), &dwBytes,
NULL, NULL) != 0)
{
dwError = WSAGetLastError() ;
}
Got it from another thread!

shayw
June 1st, 2004, 02:57 AM
Thanks Mathew.

I found that other thread, where it says that you need to include "MSTCPiP.h" file for the structure & constants definitions, however my MSVC 6.0 couldn't find this.
Is this part of an additional SDK?
Is there any additional library I need to include in my project?

Thanks again for your help.

Shay.

Mathew Joy
June 1st, 2004, 03:15 AM
Yea...the file comes with MS-SDK. However I'll post the required defs. You can paste it in your file.

#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)

struct tcp_keepalive {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};



Hope that helps!

shayw
June 1st, 2004, 04:42 AM
Hi Mathew,

Thanks a lot!
It helped me a lot!!!

BTW - is there a way to detect that the Network Cable is plugged out WITHOUT opening a socket?
Is there a way to get a notification from Windows (in other words - I need the ability to have the same functionality as the small jumping "Network" icon in the system tray...).

I really appreciate your answers!

Shay.

Mathew Joy
June 1st, 2004, 05:30 AM
I think the best way is to send an ICMP 'ping' packet, provided you know the destination IP. On good LAN network, the response to a ping will usually take < 1ms. You can use the WNetEnumResource() APIs and Co. to get the computers on the network.

:thumb: