Solved

How can I detect the connected socket disconnected (broken down) instantly ?

Posted on 2008-06-24
5
2,993 Views
Last Modified: 2013-11-20
I have a TCP socket Server in Windows PC and have 10 TCP socket Clients (Linux) on 10 PC.
After the Server and the Clients start, the Clients will connect to the Server and
does keep the connections for data transfer.
The Server listen to and create a new work thread for the connected client socket......
I want to know the 10 connections are always on, so the 10 client sockets must periodly
send the bit data to the Server to make sure that it is connected.......
I need to find the more efficient method to do that ?  Maybe I can know each connection
is down off immediately ?
0
Comment
Question by:RandallLin
  • 2
5 Comments
 
LVL 3

Expert Comment

by:winstonep
ID: 21853739
The first thing that springs to mind is to ask what OS you are using for the server?

I haven't found anything free but have a look at http://www.download.com/KeepAlive-Pro/3000-2084_4-10297409.html which is Keep Alive Pro and has a 15 day free trial. It may help.
0
 
LVL 3

Expert Comment

by:winstonep
ID: 21853747
Ok that program may not be the right one sorry... :-( I will see what I can find as I know there is something out there that can do this.
0
 
LVL 4

Accepted Solution

by:
bdunz19 earned 50 total points
ID: 21864381
Hi,
When your server, which I'm guessing is the one running on Windows, is listening for connections what sort of socket model are you using? Are you using an asyncronous model like EventSelect or IO Comptetion Ports? If you are just using the syncronous model, then when your server calls recv() it will wait untill either data is recieved from the client or the connection is closed. If recv() returns SOCKET_ERROR, then chances are the client is disconnected. You can check this further by calling WSAGetLastError(). If you are using any of the async models then you would know that you can easily recieve close messages when the clients disconnect.

Also, when you have the clients sending data periodically that works as a sort of keep alive. I've noticed that if my client programs do not do this then the connections will close after a while of inactivity. So in other words, using a keep alive method is not a bad idea to have in a client program, but the server should always be aware immediatly when the client disconnects using the methods I described above.

Hope this helps,
Brandon
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
ID: 21893284
>>>> so the 10 client sockets must periodly
>>>> send the bit data to the Server to make sure that it is connected.......
If the Server has a own thread for each connection, the thread should call select with timeout within the infinite loop. If the connection is broken, select would return SOCKET_ERROR (see th comment of bdunz19 above). If not, you can count the timeouts happened since the last receive and break the connection after having waited for too long.

Note, if not using native sockets but the Windows API, select is either WSAEventSelect or WSAAsyncSelect.

Regards, Alex
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question