views:

351

answers:

2

I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out that when compiled on Windows, my sockets are non-blocking by default, while on OS X they are blocking by default. How do I force a socket into blocking mode on Windows? Do they normally default to non-blocking mode or is something terribly wrong?

My code is based in part on these simple examples: http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoClient.c http://cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoServer.c

A: 

I believe this reference may help; note, in particular, that

Although blocking operations on sockets are supported under Windows Sockets, their use is strongly discouraged. Programmers who are constrained to use blocking mode -- for example, as part of an existing application which is to be ported -- should be aware of the semantics of blocking operations in Windows Sockets.

If you're fully aware of the zillion issues listed here, and find dealing with all of them preferable to designing your program to fit in well with Windows rather than being a half-beeped port from Unix, go right ahead with a ioctlsocket call with the cmd argument set to FIONBIO and the third argument pointing to a longword worth 0. But, don't say you weren't warned;-).

Alex Martelli
So do windows programmers normally create infinite loops to constantly poll recv and other functions? That seems awfully inefficient.
oskar
Nope, async I/O (esp. network) programming is generally done through callbacks or events, most definitely **NOT** polling, and exactly because of that it's extremely efficient. winsock is in the grand old Win API tradition, so it uses messages rather than callbacks. Or, instead of select, you use http://www.sockets.com/winsock.htm#Windows_AsyncSelect and live happily ever after;-). E.g, connect returns `SOCKET_ERROR`, you checks with WSAGetLastError and get a WSAEWOULDBLOCK, then when you WSAAsyncSelect w/interest in connections, you'll get a `FD_CONNECT` when ready -- etc, etc.
Alex Martelli
Further note that programming right to winsock can bend your brain: Python's Twisted, MFC's CAsyncSocket (http://msdn.microsoft.com/en-us/library/3tbz7kf5(VS.80).aspx), ACE (cfr http://www.artima.com/articles/io_design_patterns.html), etc, etc, all offer equally powerful and higher-level (easier to use;-) async/event based APIs to Windows async IO approaches (many with cross-platform advantages as a nice vigorish;-).
Alex Martelli
Sorry, but -1 for quoting a Windows 3.1 (!) 16 bit source. This entire line of Windows versions was replaced by the Windows 95 line, which itself has been replaced since. Look at the date on top: 1993! Windows today is pre-emptive which takes away the main blocking concern
MSalters
The winsock tag indicates to me that the OP wants to know about winsock, so that's what I answered about (while recommending NOT programming to winsock but to higher abstraction layers, such as Twisted, MFC or ACE: those frameworks, of course, will use whatever's the best implementation available on the specific system they're installed on).
Alex Martelli
+1  A: 

No, plain old BSD style sockets are not non-blocking by default on Windows. See the examples in the Winsock Programmer's FAQ.

If you use MFC and its CAsyncSocket mess, then yes, it's all non-blocking, but that's a separate issue.

Warren Young