Porting Winsock to Linux Sockets Porting Winsock to Linux Sockets windows windows

Porting Winsock to Linux Sockets


Based on that list of functions, things should more or less just work. Add #if _WIN32 around the calls to WSAStartup and WSACleanup (the linux equivalent is to not do anything, the sockets library is initialized automatically).

You also might need some OS-dependent code when setting socket options, some of them are the same, some aren't, and the types might be different.


It will depend if you use any windows specific networking functionality or if you're just using mostly the mostly BSD compatible API.

So, if you're using overlapped I/O and I/O completion ports and other advanced parts of the Winsock API then things will be very difficult to port and if you're just using the BSD compatible stuff then it should be easy to write a thin translation layer or even just have the winsock startup and shutdown stuff inside a windows specific ifdef...

This may help: http://tangentsoft.net/wskfaq/articles/bsd-compatibility.html


The only calls that make porting difficult are the WSA* calls.

WSAStartup() -> nopWSACleanup() -> nop

Socket/setsockopt -> socket/setsockopt

Under *nix, sockets are blocking by default and it's not necessary or possible to use that weird setsockopt call to fiddle with it.

ioctlsocket -> ioctl

Under *nix we don't like asynchronous sockets much and prefer to use the select() system call.

---- Rest of this answer seems only to apply to Win95 compatible winsock ----

Unfortunately as the original socket() in Winsock was broken in some cases, you probably used WSASocket() and so have to convert those calls.