Solved

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

Posted on 2008-06-24
5
2,987 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MaxSpan challenge 9 67
fix34  challenge 9 97
seriesUp challenge 7 107
wordcount challenge 11 76
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now