Looking for a proper socket keep-alive implementation

dxmedia

Member
Joined
Jul 2, 2010
Messages
16
Programming Experience
1-3
Hello, here is an detailed description of my problem:

I am building a Client/Server application in VB.NET 2008. I am looking for the best way to detect when a connection is dropped (not when the server program explicitly disconnects the client, but when the line is ‘cut’).

I do not want to make a custom protocol for sending and receiving packets every few seconds to detect if the connection is still available.

Also, I am aware about the NetworkChange.NetworkAddressChanged event, from the following article on MSDN:
http://msdn.microsoft.com/en-us/libr...sschanged.aspx

The NetworkChange.NetworkAddressChanged event detects only when the client unplugs his own network cable. It is not useful if the line is dropped somewhere else between the client and the server.

I know the sockets have a KeepAlive option:
myTcpClient.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1)

but is very little documentation.

I need an implementation of this in VB.NET 2008, or an solution of any kind for raising an event when the connection is dropped somewhere else between the client and the server.

Here is how my code works:

Client:
Uses a TcpClient and starts an asynchonus BeginRead, with a DoRead callback that executes when data is received.

Multithreaded Server:
Has a base class for a client, that is instanced for each new connection. This class also uses async BeginRead to receive data.

So, everything is asynchronus, and I do not want to use synchronus 'Receive' calls.

I have to trouble only when the network connection is dropped, not when the connection is explicitly closed either by the client or the server, because in this case I get an exception and I know the connection has closed.

Thank you in advance.
 
Back
Top