Solved

Detect a closed connection on server side

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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

789 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