Click to See Complete Forum and Search --> : Check Socket Status


Fandu_Nagesh
January 10th, 2005, 10:13 AM
Hi All
I have Clinet (Windows ) and server (Linux) application. From server side I want to continously check whether client is alive or not. (before doing any communication with client).
I tried the same to findout using select(....). But getting TRUE results even though the Client is dead.
Please give me a code snippet or links...

Nagesh

Client: Winodows 2000 VC++6
Server: Linnux, gcc

ahoodin
January 10th, 2005, 10:37 AM
Well sounds like you want keepalive probing.
http://www.unixguide.net/network/socketfaq/4.7.shtml
Check out setsockopt and SO_KEEPALIVE.


00211 optval = 1;
00212 if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) == -1)
00213 {
00214 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_KEEPALIVE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00215 closesocket(s);
00216 return INVALID_SOCKET;
00217 }


Another option is to have the client report back every so often by sending a packet to the server, and the server logging the fact that the client is still alive.

HTH,

ahoodin

P.S. If this post helped give me a good rating. Just click the scale and click approve on the window that comes up.

ssdeveloper
January 10th, 2005, 07:32 PM
I enabled 'keepAlive' checking with WinSock library.
But the 'keepAlive' message is not blank. Actually it keeps on sending one-byte data. How can I send blank message?

Fandu_Nagesh
January 11th, 2005, 04:07 AM
Hi All,
I googled for this problem. I got getpeername(...)/select(...) as remedy for this problem.

But unfortunately these functions is also not working for me. Even though the peer is dead, these fuctions returns TRUE values.

I am not getting why the windows has not provided IsSocketConnected(...) function.

Regards
Nagesh

Mathew Joy
January 11th, 2005, 01:10 PM
In windows, SO_KEEPALIVE only sets the tcp to send the keep alive messages. In win32 this is implemented according to RFC recomendation interval of 2 hrs. AFAIR it is sending blank packet, ie only the header(atleast according the the RFC spec). To change this value you have to change the registry, which, since it is global change, is not recommended without careful study. A better option which is available on Win2000 is SIO_KEEPALIVE_VALS using ioctl(), which allows you to set the keep-alive option/interval on a per socket basis. Other options are to implement ping or a keep alive message into your app.

Fandu_Nagesh
January 12th, 2005, 02:04 AM
Hi Mathew,
Thanks for your reply.
I tried the following code snippet in server side to validate the connection with client. But unfortunately this code also returns TRUE in absence of client.


if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) == -1)
perror("Keep Alive failed:");
else
perror("Socket Alive success");


Similarly I tried the GetPeerName(..)

struct sockaddr_in servAddr;
size = sizeof(servAddr);
retVal = getpeername( sd, (struct sockaddr *) &servAddr, &size);

But this is also got failed.

Well, When you call send(..)/recv(...) (Blocking call) when the client connection is closed, It says that the connection is gracefully closed. I can use send/recv call to check out the client connection, But it would be annoying to the user after knowing the connection is closed after writing a BIG messages. I just wanted to update the connection status continously to the user before sending or receving the messages............

But these functions are not helping me out for that

Thanks in advance
Nagesh

ahoodin
January 12th, 2005, 02:14 PM
But unfortunately this code also returns TRUE in absence of client

So your not validating your clients? :confused:

You do not know if your client is currently connected? :confused:

You need to do a bit more research, and to try again to get it.
Dont just give up!

ahoodin :thumb:

Fandu_Nagesh
January 12th, 2005, 11:31 PM
Hi Ahoodin,

Your post is bit interesting.....
Well, I never give up so easily.... I will try my level best......
I belive that OS must be somthing to refect the closed connection..... Anyways....
Lets see,... I am expecting some gurus to sort out the issue....

Thanks
Nagesh :p

Mathew Joy
January 13th, 2005, 03:25 AM
I tried the following code snippet in server side to validate the connection with client. But unfortunately this code also returns TRUE in absence of client.The return value of setsocket opt doesn't have anything to do with a socket being connected or not. In other words the socket that is passed to setsockopt for SO_KEEPALIVE option need not be a connected one. This socket option only means that the socket is configured for sending keep alive messages. As I said in my previous post, the time interval is implementation specific. In win32 it is normally 2 hrs. If this socket option is enabled, if a connection is dropped due to this, an error WSAENETRESET will be returned to the subsequest call(like recv).

If you are using multiple platforms, your best bet is to implement a keep-alive message yourself. If your data passing is well structured it won't be difficult to integrate such a scheme. Otherwise it maybe a bit more complecated.

For more on structured data passing look at the following faq

How do I impose a packet scheme? (http://www.codeguru.com/forum/showthread.php?t=306399)

Mathew Joy
January 13th, 2005, 03:32 AM
Looking at your post again, if you want to check at the instant if the socket is connected, you can use the SO_CONNECT_TIME socket option. But this is MS specific option and obviously won't work in other platforms.

Chronos0
January 13th, 2005, 02:52 PM
I tried the same to findout using select(....). But getting TRUE results even though the Client is dead.I tried the following code snippet in server side to validate the connection with client. But unfortunately this code also returns TRUE in absence of client.But unfortunately these functions is also not working for me. Even though the peer is dead, these fuctions returns TRUE values.

Don't socket options return a SOCKET_ERROR if they fail?

I'm understanding that SOCKET_ERROR is a non-zero value aka "TRUE".

Fandu_Nagesh
January 14th, 2005, 01:52 AM
Well, The meaning of TRUE here is they are successfully executing without giving SOCKET_ERROR.

Nagesh

Mathew Joy
January 14th, 2005, 02:36 AM
Don't socket options return a SOCKET_ERROR if they fail?Yes the do. But more importantly, only if they fail.
I'm understanding that SOCKET_ERROR is a non-zero value aka "TRUE".The value is 0xFFFFFFFF. If it is in signed var, it will be -1.