socket connection
-
Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi
-
Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi
What state is the connection in (according to netstat)? CLOSE_WAIT? When the connection drops, recv() will either return 0 or SOCKET_ERROR (WSAGetLastError() will get the error code) - I can't remember which right off hand (brain in screen-saver mode). Either way, when you get an error on send() or recv() you can notify the 3rd application that the connection dropped. A breakpoint right after a recv() call will show you which occurs when you switch off the connection. Lost connections will linger (you'll see them in netstat) until you officially close the socket or the application. Therefore, when you detect the network down you should close the socket when unrecoverable error codes are returned (they are listed in the send()/recv() API specs). Hope this helps a little :) Mark
-
What state is the connection in (according to netstat)? CLOSE_WAIT? When the connection drops, recv() will either return 0 or SOCKET_ERROR (WSAGetLastError() will get the error code) - I can't remember which right off hand (brain in screen-saver mode). Either way, when you get an error on send() or recv() you can notify the 3rd application that the connection dropped. A breakpoint right after a recv() call will show you which occurs when you switch off the connection. Lost connections will linger (you'll see them in netstat) until you officially close the socket or the application. Therefore, when you detect the network down you should close the socket when unrecoverable error codes are returned (they are listed in the send()/recv() API specs). Hope this helps a little :) Mark
Thanks for the tip, as I mentioned before when I disconnect the wireless network of the computer so that the connection ids dropped, netstat still shows that the connection is stablished!? until I close one of the application. and as my application should receive messages from the other application through the socket, I do not understand that the connection is down. Only when IN close the second application, I get the socket closed message and in netstat I can see close_wait. Thanks,
-
Thanks for the tip, as I mentioned before when I disconnect the wireless network of the computer so that the connection ids dropped, netstat still shows that the connection is stablished!? until I close one of the application. and as my application should receive messages from the other application through the socket, I do not understand that the connection is down. Only when IN close the second application, I get the socket closed message and in netstat I can see close_wait. Thanks,
Yes, you'll see the connection in netstat because the socket is still allocated even though it may be disconnected physically. It will remain in CLOSE_WAIT (waiting for close as the name implies) until you call closesocket(). Even after calling closesocket(), if there is/was pending data on the connection it will "linger" by default for I think 120 seconds (so you'll see it in netstat for that long). To force it to close immediately you can turn off the linger option for the socket, something like:
// force hard/abortive close linger Linger; Linger.l\_onoff = 1; Linger.l\_linger = 0; ::setsockopt(hSocket, SOL\_SOCKET, SO\_LINGER, (const char \*)&Linger, sizeof(linger)); ::closesocket(hSocket); hSocket = INVALID\_SOCKET;
-
Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi
-
Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi
You could wait until the TCP/IP stack reports that the connection is down (can take very long). Alternatively send a small connection testing packet (e.g. smilar to IRC's PING/PONG mechnism), sometimes you will receive immediately an error saying that the connection is down... or you can timeout the connection if you haven't received a connection test packets from the other peer for a while. [corrected typos] :)