Reputation: 22149
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?
Upvotes: 6
Views: 2628
Reputation: 9979
Have a look at this... http://sourceforge.net/projects/cpp-sockets/
Upvotes: 1
Reputation: 905
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.
Upvotes: 2
Reputation: 26129
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:
::WSAStartup()
, to shut it down in Windows, run ::WSACleanup()
; in Linux do nothing,close()
in Linux is closesocket()
in Windows,SO_RCVBUF
and SO_SNDBUF
,::ioctlsocket()
in Windows, ::fcntl()
in Linux,<sys/socket.h>
and friends in Linux, <WinSock.h>
in Windows,::select()
to wait for data to arrive,fd_set
s are totally different on Windows/Linux; this is only relevant if you need to optimize initialization of fd_set
s, such as when adding/removing arbitrary sockets,::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.
Upvotes: 5
Reputation: 16585
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.
Upvotes: 1
Reputation: 61683
Winsocks aren't very compatible with Posix sockets:
SOCKET
. On Posix it's simply a file descriptor (int
), on which you can perform normal read()
and write()
calls.recv()
and send()
.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).
Upvotes: 3
Reputation: 2009
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.
Upvotes: 1