keepalive socket option
i need to know if a client connected to me, goes down.
i have a server socket (s1) and a client socket (s2).
i make the keepalive option in this way:
on the client:
s1.SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 5000);
s1.Connect( ... );
on the server:
m_Sck = s2.Accept(); // i'm here just i receive the request from s1
m_Sck.SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 5000);
when i stop m_Sck, s1 know this condition and i can exit from my thread.
the problem is that, if i stop s1, m_Sck don't know this.
i think this is a problem (or a bug) to set the keepalive option after the socket is connected.
i can i do?
thk.
If you have a client socket connected to a server socket, typically I would assume the server socket has an outstanding receive(..) call to accept data from the client. The receive call will return with a value of 0 if the client has closed the connection. Therefore to detect the client closign the connection you can do something like...
if (socket.Receive(...) == 0){
// the other side closed the connection
}
1st step: the msdn library says "Accept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket." new Socket!
2nd step: for this reason i have write this code:
System.Net.Sockets.Socket sck;
...
sck = mySrv.Accept();
If you set KeepAlive option here, the KeepAlive don't work, but
3th step: if you do something like this
System.Net.Sockets.Socket sck;
...
sck = new Socket(...);
// before
sck.SetSocketOption(SocketOptionLevel.Tcp,SocketOptionName.KeepAlive,5000);
sck = mySrv.Accept();
// here you have KeppeAlive setted
why? how is it possible that my new Socket give the setting of a new Accept socket?
Microsoft mistery? :-)
federico
infomedica.it
2 years later... are you still there?
No.. the code doesn't work. In fact, neither set of code seems to work.
The GetSocketOption confirms that the SetSocketOption has done something... but Ethereal / Wireshark traces show that keepAlive is not happening.
I can get "client" sockets to keepAlive... no issue.
I can't get accepted sockets on the listener end to keepAlive.
Has anyone managed to get keepAlives sending from an accepted socket connection?
M.