Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winsock 2 portability

I'm about to develop some sockets related stuff in C++ and would like the software to be as portable between Windows and Linux as possible right from the start (making it portable later is tricky.)

I've looked at different libraries, there is one for C++ from alhem.net and of course there is boost::asio. boost::asio looks very promising but would be a very big dependency for applications this small.

Is it even worth writing the stuff myself or should I just use a library? If I do it myself what would be the main pitfalls?

like image 555
Skurmedel Avatar asked May 06 '09 13:05

Skurmedel


People also ask

What is the difference between Winsock and winsock2?

Winsock2 is completely backwards compatible with the original winsock, so the only reason you would ever want to use the original winsock is when you are targeting a platform that doesn't support winsock2 (Everything that is newer than Windows 3.11 supports winsock2 I believe).

Is Winsock thread safe?

3.10 - Is Winsock thread-safe? On modern Windows stacks, yes, it is, within limits. It is safe, for instance, to have one thread calling send() and another thread calling recv() on a single socket.

What is a Winsock connection?

Winsock allows Windows programs and applications to connect to the internet through TCP/IP. Here are some of its most important features: Winsock exists as a data link layer, and is also known as winsock. dll in our computers. It's a dynamic library extension link.

Do sockets work on Windows?

Run-time requirementsWindows Sockets 2 can be used on all Windows platforms.


1 Answers

I've developed a few portable wrappers around sockets. Make sure you don't go down the crappy lane of no return that is constituted of WinSock2 events. Other than that, as I see it, the biggest differences are:

  • to start networking in Windows, you need to call ::WSAStartup(), to shut it down in Windows, run ::WSACleanup(); in Linux do nothing,
  • close() in Linux is closesocket() in Windows,
  • default buffer sizes differ between both drivers and operating systems, so make sure to set them using SO_RCVBUF and SO_SNDBUF,
  • SO_REUSEADDR steals the address on Windows, allows frequent re-opening on Linux; you'd probably only want to use this flag in Linux,
  • making a socket non-blocking uses ::ioctlsocket() in Windows, ::fcntl() in Linux,
  • the header files are different, <sys/socket.h> and friends in Linux, <WinSock.h> in Windows,
  • to go portable, the easiest way is probably to use ::select() to wait for data to arrive,
  • fd_sets are totally different on Windows/Linux; this is only relevant if you need to optimize initialization of fd_sets, such as when adding/removing arbitrary sockets,
  • in Windows, any thread hanging on the socket is released with an error code when the socket is closed, in Linux the thread remains waiting. If the thread is blocking the socket with for instance ::recvfrom(), you might consider using ::sendto() to release the stalling thread under Linux.

Everything else I ever needed just worked out of the låda.

like image 125
Jonas Byström Avatar answered Sep 20 '22 12:09

Jonas Byström