[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFT] Wrap winsock functions for improved compatibility

From: Paolo Bonzini
Subject: [RFT] Wrap winsock functions for improved compatibility
Date: Fri, 12 Sep 2008 10:28:33 +0200
User-agent: Thunderbird (Macintosh/20080707)

Hi all,

this proposed patch wraps winsock functions that take or return socket
descriptors, so that C run-time library descriptors are used instead.
This should be fully transparent to the user, except that you can close
these sockets with close, read them with read/write, and so on.

The only remaining difference is the different implementation of
FD_SETs.  However, this does not matter because (mostly out of laziness)
I haven't implemented select.  So this means that select does not work
at all.  It could be done, but it would be replaced with my poll
rewrite, which would be committed at the same time as this.  You
shouldn't use select anyway. :-)

It needs to be tested on Windows (mingw and cygwin), however.  Here is a
trivial echo server (using port 12345) using the sys_socket and
netinet_in gnulib modules.  It can be tested using netcat or telnet:

#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifdef __MSVCRT__
#include <io.h>
#include <unistd.h>


main (int argc, char **argv)
  int code, s, fd, x;
  struct sockaddr_in ia;
  socklen_t addrlen;
  char buf[1024];

  s = socket (AF_INET, SOCK_STREAM, 0);

  memset (&ia, 0, sizeof (ia));
  ia.sin_family = AF_INET;
  inet_aton ("", &ia.sin_addr);
  ia.sin_port = htons (12345);
  bind (s, (struct sockaddr *) &ia, sizeof (ia));

  x = 1;
  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));

  listen (s, 1);
  fd = accept (s, (struct sockaddr *) &ia, &addrlen);
  close (s);

  while ((x = read (fd, buf, 1024)) > 0)
    write (fd, buf, x);

  x = close (fd);
  return x < 0 ? 1 : 0;

Thanks in advance!


reply via email to

[Prev in Thread] Current Thread [Next in Thread]