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.

[707 byte] By [ffederico] at [2008-2-28]
# 1
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
}

MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2

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

ffederico at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3

I tweeked your code slightly (see below). It appears setting after the Accept still maintains the keep-alive value. Please let us know if you still see otherwise. For a complete description of socket options see this MSDN page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/sol_socket_socket_options.asp



using System;
using System.Net;
using System.Net.Sockets;

class Test
{

static void Main(String[] args)
{
Socket sck, server;

server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

server.Bind(new IPEndPoint(IPAddress.Any, 5000));
server.Listen(5);
sck = server.Accept();
sck.SetSocketOption(SocketOptionLevel.Tcp,SocketOptionName.KeepAlive,0);

Console.WriteLine("value: " + sck.GetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAlive));

}

}

MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

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.

Anonymous at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified