BUG: Starting and Stopping a Winsock Server Using SO_REUSEADDR

Last reviewed: November 21, 1997
Article ID: Q177074
The information in this article applies to:
  • Microsoft Windows NT 4.0
  • Microsoft Windows 95

SYMPTOMS

To close a Winsock server and rebind a new server to its old port you need to employ setsockopt() on the server socket with SO_REUSEADDR. However, this becomes problematic if the server spawned incoming clients into child processes for handling. If any child processes are left open with their socket connection intact, any future client connection requests to any new server will be forcefully refused (WSAECONNREFUSED).

CAUSE

When the server calls closesocket(), the Windows NT TCP/IP address object connect handler address is set to NULL, which causes TCP/IP to send a TCP/IP Reset (RST) when future clients attempt to connect to the server's port number.

When a new server is created and reopens the same port, this causes the Windows NT TCP/IP driver to create a duplicate address object with a valid connect handler address. However, connection requests are validated against the old TCP/IP address object, which still has a NULL connect handler.

The end result is that all future client connection requests are forcefully refused (WSAECONNREFUSED) as long as child processes from the original server maintain their socket connections through the original NT TCP/IP address object. Once they all close, the original object is destroyed and normal operation resumes.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a Winsock server application.

  2. Upon accepting a client connection, set its handle inheritable, spawn a child process. Actual use of the socket is irrelevant. The inherited handle keeps the socket connection alive via the additional reference.

  3. Terminate the server/parent process, recreate the server process while leaving the child process open.

  4. Attempt to connect a client to the server, it should be refused.

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q166846
   TITLE     : Cannot Reconnect to TN3270 Server with Close Listen Sockets


Additional query words: SO_REUSEADDR
Keywords : NtwkTcpip NtwkWinsock
Version : WINDOWS:95; WINNT:4.0
Platform : Win95 winnt
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.