tags:

views:

1168

answers:

10

Is there any free open source library (in C/C++) for sockets that is widely used and supports wide range of operating systems (Windows, Unix/Linux, FreeBSD etc). Just like pthreads.

Otherwise the only solution left would be to write socket wrapper for each operating system. Or would writing a wrapper against winsock and GNU C sys/socket.h libraries would be enough?

Wouldn't it be possible that I implement it against the socket library provided with GNU C. GNU C is available for wide range of platforms and my code will work in all those platforms?

Thanks for all quick replies. They were all useful and fits my needs but I have to choose one of them. Its really a matter of preference and I preferred APR. Mainly because its installed with Apache so in most cases my php extension (where i am going to use sockets) don't need user to download an extra package. Also Apache (as far as I have checked its code) uses APR, so it would be reliable. Therefore I am going to mark that reply as Answer.

+9  A: 

Consider looking at boost?

carl
boost.asio is also available as a stand-alone package. http://think-async.com/Asio/
KitsuneYMG
+6  A: 

QT if it isn't too big for you. Boost has some network code as well. wxWidgets has with wxNet a network library as well. Another lib is Clanlib. And of course SDL

Tobias Langner
+3  A: 

Yes, you will get very far with a wrapper around Winsock and standard Berkeley sockets. In fact, the differences are so small it's almost possible to do it with #ifdef directly in the code.

That is, if you're willing to work at the socket level. If you're after something more abstract, then of course wrapping it gives you a good opportunity to also hide the differences.

In particular, Winsock:

  • Requires you to "start it up" by calling WSAStartup() before any other socket function
  • Does not allow you to use plain old close() on a socket; you must use closesocket()
  • Requires that you use WSAGetLastError() to get the last error, not plain errno.

Those are the three major ones off the top of my head, there might be more.

unwind
Why? It's been done dozens of times before and I doubt you can't find an interface you like.
Chris Lutz
@Chris: Why what? :) Why work at the socket level? I don't know, sometimes it makes sense, but you still might want to be portable.
unwind
I am thinking of writing a class (abstract if it has to) which will hide the differences. I can derive from that class an implement for winsock and Linux sockets or any other library.That is if i dont find a small and efficient library.
cornerback84
@unwind: I cannot comment for Chris, but I'd ask: Why do it yourself if there are so many implementations out there?
sbi
Why wrap it when there is nothing to wrap? BSD socket calls work everywhere.
Chris Becke
@Chris Becke: that was sort of my point, yeah. There are some minor differences on Windows (you can't close() a socket, for instance).
unwind
Wouldn't it be possible that I implement it against the socket library provided with GNU C. GNU C is available for wide range of platforms and my code will work in all those platforms.
cornerback84
+2  A: 

I believe both the Apache Portable Runtime and GTK+'s GLib libraries have socket APIs. Since your question is tagged c and c++ I suspect you really want C++-centric answers, but both of these are good as pure C libraries.

Chris Lutz
Apache Portable Runtime is a good suggestion. Apache also uses it, so I think it would be stable, scalable and with good performance.GLib has full support of UNIX but support for windows in partial.
cornerback84
+4  A: 

ACE may Help you !

This tutorial provides an overview of the ACE OS adaptation layer and the design and use of its C++ Socket wrappers

Developing Efficient and Portable Communication Software with ACE and C++

sat
If you take a look at ACE, don't get overwhelmed by the size of it. The core ACE library has easy to use TCP and UDP classes. The online documentation is pretty much reference. Get the book "ACE Programmer's Guide" to learn how to use it.
zooropa
A: 

If you want a very light, C++ only, iostreams-style wrapper around BSD sockets, you could consider skstream - it's simple and works well. Of course, the iostreams interface itself leaves much to be desired. skstream does wrap select and the very low-level socket handling up for you quite nicely, though.

James Turner
A: 

There is only one correct answer here. OpenSSL. Because once you have socket code, youre going to want to make SSL connections too. If you are already working in a particular cross platform framework, OpenSSL bindings might already be avaialable. I don't think theres much excuse now to not SSL/TLS enable your application from the get-go.

Chris Becke
sorry but we are talking about socket programming, not session layer or security.
EffoStaff Effo
+2  A: 

Use boost::asio. Very good library. Follow this link http://www.boost.org/doc/libs/1%5F40%5F0/doc/html/boost%5Fasio.html

Davit Siradeghyan
+3  A: 

Some links for you :
C++ Sockets Library
C++ Socket library - ComPP 1.3
SimpleSockets
libmsocket
Komodia TCP/IP library
STLplus C++ library

Framework
nitro++

lsalamon
Thanks for the links.
cornerback84
+2  A: 

Another option you can try is Poco. Poco has a lot more than sockets as well, so if you need other stuff it can provide as well.

Comparing it to other toolkits:

  1. Qt has GUI and some very sophisticated features you won't find in Poco, but it's a lot bigger.
  2. Boost doesn't have as much as Poco in the way of actual functionality, but it has libraries like reference wrappers, MPL and preprocessor which cannot be found anywhere else.
  3. I don't remember ACE very well, but I remember it was very very big in terms of source code and components. ACE seems to be used for very high performance network applications, so it may be overkill. But then again, I'm not too sure.

Just my two cents

blwy10
From the list of supported platforms, I cannot find UNIX/Linux. On the other hand they have put more emphasis on embedded platforms.
cornerback84
Poco's supported platforms? They do support Unix/Linux. It's on the home page at the bottom: Linux; HP-UX; Tru64; Solaris. All these are Unix or Unix-like operating systems.
blwy10