?
Solved

closesocket() problem

Posted on 2003-03-28
7
Medium Priority
?
635 Views
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?
0
Comment
Question by:desktop2
  • 3
  • 3
7 Comments
 
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
0
 

Author Comment

by:desktop2
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

 
0
 
LVL 3

Expert Comment

by:TascoDLX
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?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

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

Expert Comment

by:TascoDLX
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.
0
 

Author Comment

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

Accepted Solution

by:
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:

http://tangentsoft.net/wskfaq/

Good luck!
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

578 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