Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-24
5
Medium Priority
?
3,001 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 200 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 200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

885 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