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

x
?
Solved

Detect a closed connection on server side

Posted on 1998-01-28
4
Medium Priority
?
243 Views
Last Modified: 2013-12-23
Hi.

I have a very regular server application. It uses TCP,  listen() to a public port, select() file descriptor set and accept() new connections. At the same time, it maintains all these client connections.

Sometimes, the clients may close their connection without informing the server, like power off the PC, kill the application.

Now my question is:
How can the server side detect those closed connections safely and precisely ?

Actually, this application is running on NT and it uses winsock. But it is ported from Unix and asynchronous functions are not used. So all the functions are still BSD socket functions. I know I can solve this problem by checking WM_CLOSE message on the socket. But we don't want to change the code at this time. So I guess it is better to ask Unix expert to answer this question.

Thanks in advance.

Mike
0
Comment
Question by:mikechen
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mlev earned 200 total points
ID: 1583091
I assume you do a select() on all those client connections, right? And when select() returns, you probably check connections for incoming data (and do a read() to receive data). So, when a socket is closed by the peer, it will look just like if some data arrived, except that when you try to read() from the socket, you will get 0 bytes. When read() returns 0, you can safely close() that socket. This is described in the Unix socket FAQ, I think.
0
 

Author Comment

by:mikechen
ID: 1583092
Hi, Mlev.

Thank you very much for your response.

Just one question, are you sure there won't be any pending data left in the socket ? Like this senario, I don't call select() in 1 second. During this 1 second, the client site send a package and terminate itself. On the server, it calls select() after all these happen. Will select() returns twice ? Or just once ?

Thanks,

Mike

0
 
LVL 2

Expert Comment

by:mlev
ID: 1583093
It will return indefinitely, until you remove the terminated connection from the select pool. (I.e. it will act as though there's always pending data on it).
If the client sent a package and then terminated, you will first see that package and only the second read() will return 0.
0
 

Author Comment

by:mikechen
ID: 1583094
Thank you, Mlev.

Mike
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

824 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