Porting Winsock to Linux Sockets

Mike Bailey picture Mike Bailey · Jan 6, 2011 · Viewed 25.2k times · Source

I have a program that does some networking using Winsock, and one of our requirements right now is to port over our program to Linux. The only thing stopping us from doing this is Winsock.

My question is: How easy can I port this over to a Linux implementation?

Are there any pitfalls I should be aware of, and if I simply include the appropriate header files, what sort of things will I have to be sure to handle?

Thanks for any help!

I'd post code but I can't unfortunately due to legal reasons. But, our code does use the following:

WSAStartup(..)
WSACleanup(..)
Socket(..)
sendto(..)
recvfrom(..)
ioctlsocket(..)
setsocketopt(..)

Answer

Ben Voigt picture Ben Voigt · Jan 6, 2011

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.