便携式轻型 C++套接字包装器
我真的认为这会更容易找到...
我需要一个便携式 C++ 套接字包装器。我计划将它用于 Windows 服务器应用程序和将在运行 ulinux (或类似设备)的嵌入式设备上运行的客户端。我会使用 Boost,但我需要它是轻量级的并且易于添加到嵌入式设备项目中。
另外我希望它成为一个“更高级别”的包装器...因此它启动一个后台线程来读取数据并通知回调...
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
看看 ENet http://enet.bespin.org/ 它非常轻量级且便携,并且可以在UDP 之上,可选支持可靠数据包。它易于使用,API 是低级的,并且性能开销很小。您对内存管理有高度的控制,如果网络是您的瓶颈并且您使用的 malloc/new 实现在多线程下性能不佳,那么这可能会很好。
“最佳”实现高级线程并不难,因为有对阻塞接收的可选支持,并且库是一个“库”而不是框架,因此您是决策者而不是库。
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.
也许你可以看看 http://www.pt-framework.org/
Perhaps you can have a look at http://www.pt-framework.org/
老问题,但对于 C++、BSD 风格的同步套接字,这大约是您能找到的最小行李包装器
http://code.google.com/p/ ting/source/browse/trunk/src/ting/net/
它确实有例外。您可以制作一个更轻量级的模板库,作为仅包含标头的模板库,并且可以将例外设置为可选,但这会稍微改变 API
POCO 网络类非常相似,但确实需要来自 Poco 库其他部分的更多依赖项
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
我个人正在为 TCP 和串行套接字创建自己的 AsIO 包装器,我首先查看以下教程:
和
https://objectcomputing.com/resources/publications/mnb/multi-platform-serial-interface-using-boost-a-gps-sensor-and-opendds-part-i/
我发现第一个非常有用且易于理解。
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.
C++CSP2
用过很喜欢。稳定、强大
C++CSP2
Used it loved it. Stable and powerful
我建议Boost.Asio。尽管有它的名称,但您并不被迫使用异步 I/O。正如您的问题所暗示的那样,您可以使用同步 I/O 和线程。
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.
只要学会直接使用socket API就可以了。然后您可以轻松地自己包裹它。这并不那么难,您可以从Beej 的优秀指南开始。正如 Beej 所说:
在他的指南中,他详细介绍了在 Windows 和 *nix 系统中获得相同 API 所需要做的非常小的补充。
一旦你学会了,如果你愿意的话,可以自己包装它。然后你就可以精确地控制你想要的“轻量”程度。
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:
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.
如果您确实不喜欢 Boost asio,那么您可能会喜欢 dlib 中的套接字支持。它更简单,因为它使用传统的阻塞 IO 和线程,而不是 asio 的异步前摄器模式。例如,它可以轻松创建一个从 iostream 读取和写入的线程 TCP 服务器。例如,请参阅此示例。或者,如果不充当服务器,您可以制作一个简单的 iosockstream 。
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.
我知道这已经很旧了,但是在下面的位置有一个非常漂亮且简单的实现,我将其用于个人用途。不久前实现了我自己的包装器,但丢失了代码,并在网上发现了这个比我的好得多的包装器:
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/