views:

465

answers:

6

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?

+1  A: 

Honestly, I'd use boost::asio as a first preference. If you really want to get down and dirty with the sockets API, you can use the standard BSD-style sockets API on both Windows and Linux - it's just that on Windows you'll have to link to (and initialize) Winsock2, whereas on Linux you won't have a separate library to link against.

Harper Shelby
Thanks for the advice. +1 this, can only mark one answer :(
Skurmedel
+1  A: 

How much socket stuff will you be using? I've done several apps where the socket stuff was pretty high level (open, read, write), and worked perfectly from Windows to Linux. If it's more than that - go with boost.

Jeff
At first, it would be very low level, like open a connection, write something, read, quit. It could probably grow to include Http-stuff though.
Skurmedel
Thanks for the advice. +1 this, can only mark one answer :(
Skurmedel
+2  A: 

Winsocks aren't very compatible with Posix sockets:

  • In Winsocks a socket is of type SOCKET. On Posix it's simply a file descriptor (int), on which you can perform normal read() and write() calls.
  • They don't return errors the same way.
  • They don't support some options on recv() and send().
  • You have to initialize and unitialize the Winsocks library with two specials functions.
  • I don't think you can close Windows sockets with shutdown() or close(). It's something like closesocket() instead.

There must be more differences, but that's what I can remember right now. If you want portability with Winsocks, you'll have a small library for closing a socket, printing an error message and so on.

I'd probably go with boost::asio, personnally (I've never used it, though).

Bastien Léonard
Ok, I'll see what I do. boost::asio has an ICMP interface too which is very nice so currently it looks like I'll use that.
Skurmedel
shutdown() is a valid function for Windows sockets. Are you correct that closesocket() needs to be used, though.
Remy Lebeau - TeamB
+2  A: 

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.

Jonas Byström
Very useful advice. Tackar och bugar ;).
Skurmedel
You're most välkommen!
Jonas Byström
+1  A: 

Take a look at the "Adaptive Communications Environment" (ACE) library: (ACE Home Page) It provides some nice abstractions and a lot of flexibility all rolled up in a portable library that supports Windows, MacOS and Linux. It has a bit of a steep learning curve, but I got very good value from it.

Bruce Ikin
Thanks for the suggestion.
Skurmedel
+1  A: 

Have a look at this... http://sourceforge.net/projects/cpp-sockets/

Partial
Seems quite useful. Thanks mate. It seems rather old though, last updated 2003? But maybe it still works well.
Skurmedel