Listening sockets not always closing?

Craig Davison craig@darkcalgary.com
Tue Dec 20 21:32:00 GMT 2005


Hello,
I'm running cygwin 1.5.18 on a number of versions of Windows, and I may
have found a problem closing sockets. I'm seeing this problem on Windows
2000 (no SP) and Windows 2000 SP1, but not on Windows 2000 SP2, SP3 or
SP4, and Windows XP and XP SP2 are also unaffected.

I created a small test program to illustrate this. Basically, in a loop, I
create a SOCK_STREAM socket, call bind() and then listen(), check the
output of netstat, and then close the socket. It works on several
versions of Linux, and Windows >= 2000 SP 2, but bind fails with
EADDRINUSE the second time through the loop on Windows <= 2000 SP1.

I think this is a cygwin-specific problem because if I try the same thing
with a native windows application using winsock, it works. (I can mail out
this code if anyone wants)

The code for my test program is attached. If I'm doing something stupid
here, I apologize for wasting everyone's time.
-------------- next part --------------
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define PORT 12345

void netstat()
{
  char netstat_cmd[100];
  sprintf (netstat_cmd, "netstat -an -p tcp | /bin/grep %d", PORT);
  system (netstat_cmd);
}

int main()
{
  int i;
  for (i = 0; i < 10; i++) {
    struct sockaddr_in saddr;
    int s;
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = 0;
    saddr.sin_port = htons(PORT);

    s = socket (PF_INET, SOCK_STREAM, 0);
    if (s == -1) {
      perror ("socket");
      exit (1);
    }
    if (bind (s, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
      perror ("bind");
      exit (1);
    }
    if (listen (s, 100) == -1) {
      perror ("listen");
      exit (1);
    }
    printf ("Called listen\n");
    sleep (1);
    netstat();
    if (close (s) == -1) {
      perror ("close");
      exit (1);
    }
    printf ("Called close\n");
  }

  return 0;
}

-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list