The Mingw32 environment is a great thing. It lets me develop an app for Windows on my Linux computers without even having to think about the platform differences – apart from directory separator and stuff like that.
Well, most often. Looks like the Winsock2 API is really, really far off the POSIX way of doing things. First, before even thinking about using the network, you have to issue a special init call:
WSAStartup(MAKEWORD(2,0),&wsaData);
Then, you have to worry about checking your sockets for errors. The POSIX way of this is
if ((sock = connect(…)) < 0), meaning socket numbers are positive. The Winsock2 way is:
if ((sock = connect(…)) == INVALID_SOCKET). More #ifdef ugliness.
At last, when you finally managed to get a connection established, you can try to read() or write() to it. Which gives errors. Better use recv() or send(). Both couple of functions taking almost identical parameters. I must have missed another Windows subtlety concerning this one. I can’t believe it doesn’t work.
And if you want to debug network operations, don’t bother using perror(), use WSAGetLastError(). Have fun with #ifdefs if you want debug on both platforms. I won’t elaborate on the stupid Java-like naming scheme in C files, either; it just gets on my nerves.
Oh yeah, and when you’re done, don’t forget to call the releasing stuff: WSACleanup(). Docs don’t say what happen if you don’t, but this is Windows. Better not irritate the thing.
Ah, but then, you don’t know the history of WinSock.
WinSock was an API introduced by a who’s who of network-related companies who needed a common API for networking. Microsoft was one of those companies, and seeing and realizing the popularity of the API, it was one of its implementors. Ofcourse it included its stack for free in its OS, and the rest is history.
The originators of the WinSock API stayed very close to the BSD sockets API – most of those people had a BSD/UNIX background. Almost all differences between WinSock and BSD sockets are because of the limitations of WinSock’s first platform.
In Win32 sockets are actually file handles, and they are closed when a process closes.
You may want to look at sockets.com; this site has been there since the standardisation of WinSock 1.x, and the domain’s owner, Bob Quinn, was a founding member of WinSock.
If you really need a very quick overview of WinSock and potential pitfalls, check the lame list.
Thanks, I had found the site, but missed the lame list :-)
From the lame list:
# Single byte send()s and recv()s.
Festering in a pool of lameness.
I fail to see why, and proudly do it until optimization time has come :-)
The network stack buffers too (unless you ioctl() it). So sending one byte a time is a waste of CPU cycles. Time it. :-)
You meant that kernel/user separation is not good :) ?
multiplying system calls in general will be a waste of CPU cycles.
long life to (ms?-)DOS :)