Win32 Port

Ralf Habacker ralf.habacker at
Tue May 2 02:36:35 PDT 2006

Tor Lillqvist schrieb:
>  > > Just curious - is your isSocket() something like _get_osfhandle()<0 ? Or
>  > > did you find a better WinAPI that answers the handle type precisely ?
>  > I used the return code of getsockopt(), which is zero for socket file
>  > types  and  -1  for non socket type.
> But what if the same integer happens to be both a socket and a file
> descriptor...? Overlap (i.e. smallish socket numbers) can very well
> happen. I once had a test program to check this but can't find it
> right now, but IIRC you didn't even have to try very hard to get
> overlap (no need to open a stupendous amount of files or sockets).
How do you have performed the test if you don't open sockets or files ?

The following test program shows me that on Windows XP socket and file 
descriptors are unique.
#include <winsock2.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

int main()
  WORD wVersionRequested;
  WSADATA wsaData;

  wVersionRequested = MAKEWORD( 2, 2 );
  int err = WSAStartup( wVersionRequested, &wsaData );
  int listen_fd;
  int file_fd;
  int i;
  for (i=0; i < 100; i++) {
    listen_fd = socket (AF_INET, SOCK_STREAM, 0);
    file_fd = open("test.c", O_RDONLY);
    printf("listen_fd=%d file_fd=%d  ",listen_fd, file_fd);
file_handle=%d\n",_get_osfhandle(listen_fd), _get_osfhandle(file_fd));
c:\mingw\bin\gcc -o test test.c  -lws2_32

listen_fd=1784 file_fd=3  listen_handle=-1 file_handle=1776
listen_fd=1772 file_fd=4  listen_handle=-1 file_handle=1768
listen_fd=1764 file_fd=5  listen_handle=-1 file_handle=1760
listen_fd=1756 file_fd=6  listen_handle=-1 file_handle=1752
listen_fd=1748 file_fd=7  listen_handle=-1 file_handle=1744
listen_fd=1740 file_fd=8  listen_handle=-1 file_handle=1736
listen_fd=1732 file_fd=9  listen_handle=-1 file_handle=1728
listen_fd=1724 file_fd=10  listen_handle=-1 file_handle=1720
listen_fd=1716 file_fd=11  listen_handle=-1 file_handle=1712
listen_fd=1708 file_fd=12  listen_handle=-1 file_handle=1704
listen_fd=996 file_fd=101  listen_handle=-1 file_handle=992
listen_fd=988 file_fd=102  listen_handle=-1 file_handle=984

According to it looks 
like socket descriptors are operating-system file handle and file 
descriptors are at a higher level, but has an additional 
operating-system file handle because the handle returned by 
_get_osfhandle() fits exact in the gap between to listen_fd's.

Does other windows based operations systems gives other results ?
> Having the file/socket separation spelled out in the API is
> unfortunately the only reliable way to handle this.
> --tml
> _______________________________________________
> dbus mailing list
> dbus at

More information about the dbus mailing list