Christopher Dolan
Christopher Dolan

Reputation: 179

Making a Nonblocking socket for WinSocks and *nix

In C/C++, how would I turn a blocking socket into a non blocking socket in both WinSocks and *nix; so that select() would work correctly. You can use the pre-processor for the platform specific code.

Upvotes: 6

Views: 3451

Answers (2)

wnoise
wnoise

Reputation: 9922

select() is supposed to work on blocking sockets. It returns when a read() would return immediately, which is always the case with non-blocking sockets.

Upvotes: 5

hazzen
hazzen

Reputation: 17486

On linux:

fcntl(fd, F_SETFL, O_NONBLOCK);

Windows:

u_long on = 1;
ioctlsocket(fd, FIONBIO, &on);

Upvotes: 7

Related Questions