Solved

Detect a closed connection on server side

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Finding a free PC on campus 6 54
Wireshark 7 55
Cisco switch SVI 17 42
What's the problem with my DSL? 4 29
Some time ago I was asked to set up a web portal PC to put at our entrance. When customers arrive, they could see a webpage 'promoting' our company. So I tried to set up a windows 7 PC as a kiosk PC.......... I will spare you all the annoyances I…
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.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now