Saturday, July 05, 2008 7:20 AMWSAPoll() is supposed to be identical to BSD's poll() function, see http://blogs.msdn.com/wndp/archive/2006/10/26/WSAPoll.aspx and the MSDN docs. According to my test, it's not. It does not report failed connections!
1. Create a non-blocking socket.
2. Connect to a address&port with no listener.
3. Call WSAPoll() with any valid flags set in the events field (eg. POLLIN|POLLOUT but I've tried every bit set, except the invalid bits), and time out to -1.
WSAPoll will never return!
I've tried this on FreeBSD 7.0, Linux (CentOS 5.2), and OpenSolaris 2008.05. They all report failed connections. Here's the example code which fails (blocks in WSAPoll) on Windows Vista SP1:
typedef SOCKET sock_t;
#define SOCKERR GetLastError()
#define sockpoll WSAPoll
#define SOCKCONNBLOCK WSAEWOULDBLOCK
#define SOCKCONNREFUSED WSAECONNREFUSED
#define sockpoll poll
#define SOCKCONNBLOCK EINPROGRESS
#define SOCKCONNREFUSED ECONNREFUSED
#define SOCKERR errno
typedef int sock_t;
#define ASSERT(x) \
if (!(x)) \
printf("ASSERT(%s) failed\n", #x); \
} while (0)
ASSERT(WSAStartup(0x202, &wsaData) == 0);
sock_t sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ASSERT(sock != (sock_t)-1);
ULONG on = 1;
ASSERT(ioctlsocket(sock, FIONBIO, &on) != -1);
int on = 1;
ASSERT(ioctl(sock, FIONBIO, &on) != -1);
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(0xFEFE);
sa.sin_addr.s_addr = htonl(0x7F000001);
int ret = connect(sock, (sockaddr*)&sa, sizeof(sa));
ASSERT(ret == 0 || SOCKERR == SOCKCONNBLOCK ||
SOCKERR == SOCKCONNREFUSED);
if (ret != 0 && SOCKERR == SOCKCONNBLOCK)
printf("Waiting for connect to succeed or fail...\n");
fds.fd = sock;
/* Setting these flags will fail immediately: POLLPRI POLLWRBAND POLLERR POLLHUP POLLNVAL */
fds.events = POLLRDNORM | POLLRDBAND | POLLIN /*| POLLPRI*/ | POLLWRNORM | POLLOUT /*| POLLWRBAND*/ /*| POLLERR*/ /*| POLLHUP*/ /*| POLLNVAL*/;
fds.revents = -1;
ret = sockpoll(&fds, 1, -1);
printf("poll returned: %d\n", ret);
if (ret == -1)
printf("ERROR: %d\n", SOCKERR);
Sunday, July 20, 2008 12:11 AMI talked to Bill Gates, and he confirmed that they fucked up.
Tuesday, September 14, 2010 7:34 PMI've got the same problem. Any ideas ? It looks like a Windows bug and it hasn't been fixed since 2008 ...
Friday, October 05, 2012 12:57 PM
For Microsoft, it is a known issue:
"Windows 8 Bugs 309411 - WSAPoll does not report failed connections
8/3/2011 6:53 PM Resolved as Won't Fix by muraris
Has been like this forever and people are already used to it."
"The recommendation for now is to not use the WSAPoll function it in case you encounter this issue, but rather the other Net-API functions."
See http://curl.haxx.se/mail/lib-2012-10/0038.html for a further discussion.