Solved

Detect a closed connection on server side

Posted on 1998-01-28
4
221 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 50 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Outlook keeps opened file locked 2 71
logging buffered 8 48
How to calculate the netmask 5 55
Setting up static routes to  sonicwll 4 41
If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

810 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