Solved

Detect a closed connection on server side

Posted on 1998-01-28
4
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Make the most of your online learning experience.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
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…

691 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