19

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?

rusbi
  • 530
  • 1
  • 6
  • 17
  • 3
    I think Boost is good enough for you. – stnr Dec 17 '10 at 15:56
  • 2
    @stavnir: I agree with you, but he'll probably want somebody to address his fear that Boost.Asio isn't "lightweight." – Steve M Dec 17 '10 at 16:00
  • 2
    IIRC, threading on Windows and Linux isn't really the same thing, so your so-called "higher level wrapper" isn't really a wrapper any more, but leaning towards a framework. I don't think you'll find something prefab that does exactly what you want, so you might want to write your own. That's what we did at work (just for sockets, no thread server), and it's held up fine (can't put it here due to license issues). – Mike DeSimone Dec 17 '10 at 16:12
  • People have always carefully handpicked parts of boost that are lightweight. Such are usually proposed to library of next standard. asio was proposed into TR2 AFAIK. Note that he wants threads also portably. – Öö Tiib Dec 17 '10 at 16:14

9 Answers9

12

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.

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • 14
    I found boost to be excessively complex for what should be a simple task. – Jay Dec 17 '10 at 20:43
  • The problem is that it uses an unusual programming paradigm which is simply not well known. So at first asio may seem complex but if you read its tutorial and introduction you will find out that it is actually quite simple and powerful. – Davis King Dec 17 '10 at 22:44
  • @Jay have you gone through the examples? The [blocking echo client and server](http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/echo/blocking_tcp_echo_client.cpp) are about as simple as it gets. @Davis King I agree the proactor pattern enforced by the asynchronous operations is not excessively common, but the library does not force that pattern on its users. It is possible to use it in a synchronous fashion too. – Sam Miller Dec 17 '10 at 23:40
  • 5
    You consider Boost lightweight? – Proxy Sep 17 '14 at 12:47
4

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.

Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • 6
    I know how to use it... but its a pain in the *** :) – rusbi Dec 17 '10 at 16:05
  • 29
    This is terrible advice. The BSD sockets API is full of surprising pitfalls (e.g. OOB data is a broken hack since TCP doesn't actually support it) and is subtly different between different platforms. Things which might at first seem the same on windows and linux end up being different in important ways which ultimately result in major security bugs or other problems when not explicitly taken into account. Even on Unix systems there are important and subtle variations in how the API is implemented. See Unix Network Programming by Stevens for a very in-depth discussion of these issues. – Davis King Dec 17 '10 at 22:40
  • 1
    It seems that I will have to write my own object/wrapper, so I'll mark this as the best answer... – rusbi Dec 21 '10 at 11:33
  • I've developed such a wrapper in C for myself which I use in several projects for years. And really it's a very gloomy to do that in compare with .NET, for example. That's true that there are very many pitfalls, incompatibilities and so on. – Yury Oct 18 '12 at 13:14
2

I know this is old, but there is a very nice and simple implementation in below location which I'm using for personal use. Had implemented my own wrapper a while back but lost the code and found this one online which is much better than mine:

http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/

djaa2807
  • 39
  • 1
2

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.

Davis King
  • 4,731
  • 1
  • 25
  • 26
1

Take a look at ENet http://enet.bespin.org/ it is very lightweight and portable and works on top of UDP, with optional support for reliable packets. It is easy to use, the API is low-level and with little performance overhead. You have a high degree of control over the memory management, which could be good if networking is a bottleneck for you and the malloc/new implementation you use performs badly under multithreading.

It would not be that hard to implement your high level thread “optimally”, since there is optional support for blocking receive and the library is a “library” and not a framework therefore you are the decision maker instead of the library.

Magnus Andermo
  • 353
  • 4
  • 8
0

Old question, but for C++, BSD style synchronous sockets this is about as minimal baggage wrapper as you can find http://code.google.com/p/ting/source/browse/trunk/src/ting/net/

It does come with exceptions. You could make a bit more lightweight one as a header-only template library, and maybe make exceptions optional, but that would change the API a bit

POCO network classes are quite similar, but do require more dependencies from other parts of the Poco lib

kert
  • 2,161
  • 21
  • 22
0

Perhaps you can have a look at http://www.pt-framework.org/

SebastianK
  • 3,582
  • 3
  • 30
  • 48
0

I'm personally creating my own AsIO wrapper for both TCP and Serial sockets, and I started by reviewing the following tutorial:

https://www.gamedev.net/blogs/blog/950-they-dont-teach-this-stuff-in-school/

and

https://objectcomputing.com/resources/publications/mnb/multi-platform-serial-interfacing-using-boost-a-gps-sensor-and-opendds-part-i/

I found the first one very useful and simple to understand.

Ugo Giordano
  • 373
  • 3
  • 11
-1

C++CSP2

Used it loved it. Stable and powerful

Matt Kline
  • 10,149
  • 7
  • 50
  • 87
Boris
  • 1,311
  • 13
  • 39
  • This is a concurrency library, which whilst it may abstract network connectivity, does not provide a direct c++ wrapper for socket connectivity. – Bilko Oct 19 '16 at 05:06