Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable lightweight C++ sockets wrapper

I really thought this would be easier to find...

I need a portable c++ sockets wrapper. I'm planning to use it for a windows server application and a client that will be running on a embedded device running ulinux (or something similar). I would use Boost but I need it to be lightweight and easy to add to the embedded device project.

Also I would like it to be a "higher level" wrapper... so it starts a background thread to read data and informs be over a callback...

Any ideas?

like image 840
rusbi Avatar asked Dec 17 '10 15:12

rusbi


Video Answer


3 Answers

I'd suggest Boost.Asio. Despite it's name, you are not forced to use asynchronous I/O. You could use synchronous I/O and threads, as your question implies.

Boost.Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach.

like image 133
Sam Miller Avatar answered Sep 20 '22 18:09

Sam Miller


Just learn to use the socket API directly. You can then easily wrap it yourself. It's not that hard, and you can get started with Beej's excellent guide. As Beej says:

The sockets API, though started by the Berkeley folk, has been ported to many many platforms, including Unix, Linux, and even Windows.

In his guide he details the very small addition you need to do to get the same API in Windows and *nix systems.

Once you've learned, wrap it yourself if you're so inclined. Then you can control exactly how "lightweight" you want it.

like image 42
Doug T. Avatar answered Sep 19 '22 18:09

Doug T.


If you really don't like Boost asio then you might like the sockets support in dlib. It is simpler in the sense that it uses traditional blocking IO and threads rather than asio's asynchronous proactor pattern. For example, it makes it easy to make a threaded TCP server that reads and writes from the iostreams. See this example for instance. Or you can just make a simple iosockstream if not acting as a server.

like image 22
Davis King Avatar answered Sep 19 '22 18:09

Davis King