closesocket() problem

Posted on 2003-03-28
Medium Priority
Last Modified: 2013-12-03
Sometimes there is a problem to close socket when client and server close socket same time:

What happens on server:
1) Server close socket 222
2) FD_CLOSE received for socket 222
3) New client accepted on socket 222 (since socket 222 is already closed above)
4) Server close new socket 222 (by mistake - in response to FD_CLOSE above)

How can I handle this?
Question by:desktop2
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
  • 3
  • 3
LVL 23

Expert Comment

by:Roshan Davis
ID: 8229532
Please search "PRB: Socket Messages Appear to Be Sent with the Wrong Message ID" in MSDN.

Good Luck

Author Comment

ID: 8229548
The search returns
 Download the Microsoft Windows Socket 2 Update
 Download the Winsock/DNS Upgrade 1.2 for PPTP
 Platform SDK Documentation: Windows Sockets Version 2
 MSDN Article: Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports


Expert Comment

ID: 8236459
> What happens on server:
> 1) Server close socket 222
> 2) FD_CLOSE received for socket 222
> 3) New client accepted on socket 222 (since socket 222 is already closed above)
> 4) Server close new socket 222 (by mistake - in response to FD_CLOSE above)

The server is closing the socket too soon.  It should close the connected socket only upon receiving FD_CLOSE.

In your example, step 1 should not occur.

Do you have a good reason for closing the socket early?
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Author Comment

ID: 8236487
It is rare case...
 Server sends reply and close socket,
but then it receive FD_CLOSE

Expert Comment

ID: 8236813
Short answer:

Server should call shutdown() instead, and shouldn't call closesocket() before receiving FD_CLOSE.

Long answer:

In effect, the server is disconnecting from the client.

Here's how a disconnection occurs:

- Side A calls shutdown() -> Side B receives FD_CLOSE

= Side B's response to FD_CLOSE:
- Side B calls shutdown() -> Side A receives FD_CLOSE
- Side B calls closesocket()

= Side A's response to FD_CLOSE:
- Side A calls closesocket()

(Of course, as part of a "graceful" close, both sides should recv() all data available before calling their coresponding closesocket()'s.)

Ideally, both sides should call shutdown(), and both sides should call closesocket() in response to FD_CLOSE.

To handle the possibility of both sides disconnecting, use a boolean flag to keep track of whether or not your client/server called shutdown() yet and check it during your FD_CLOSE handler.

Author Comment

ID: 8238097
It is working!
I'm not sure about "graceful" close ... Should I set same option SO_LINGER in this case?

Accepted Solution

TascoDLX earned 500 total points
ID: 8242270
You shouldn't need to set the SO_LINGER option.  It should default to a normal timeout.

Here's how to close a connection gracefully:

- After your app is finished sending data, it calls shutdown() with how=1.

- Before closing the socket, the app loops on recv() to read all remaining data (until it returns 0 or an error).

- Having finished sending and receiving data, your app calls closesocket().

With asynchronous notification, things are a bit different.

After calling shutdown(), the app may receive more FD_READ messages, indicating unread data.

If the app isn't expecting more data, it doesn't have to wait for FD_READ or FD_CLOSE.  It can disable asynchronous notification on the socket by calling WSAAsyncSelect(), recv() the remaining data into a discardable buffer, and closesocket().

If you wanted to set SO_LINGER with a custom timeout value, you would do so before calling closesocket(), but as I said, usually the default is sufficient.

The asynchronous method above is used by Bob Quinn in the book "Windows Sockets Network Programming" by Bob Quinn and Dave Shute.  But alot of this is common knowledge and can be obtained in other places such as the Winsock Programmer's FAQ, here:


Good luck!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 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