便携式轻量级 C++ 套接字包装器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4472175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Portable lightweight C++ sockets wrapper
提问by rusbi
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.
我需要一个便携式 C++ 套接字包装器。我计划将它用于 Windows 服务器应用程序和将在运行 ulinux(或类似的东西)的嵌入式设备上运行的客户端。我会使用 Boost,但我需要它轻巧且易于添加到嵌入式设备项目中。
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?
有任何想法吗?
采纳答案by Doug T.
Just learn to use the socket API directly. You can then easily wrap it yourself. It's not thathard, and you can get started with Beej's excellent guide. As Beej says:
直接学习使用socket API即可。然后,您可以轻松地自己包装它。这并不是说辛苦了,你可以开始使用Beej的优秀导游。正如 Beej 所说:
The sockets API, though started by the Berkeley folk, has been ported to many many platforms, including Unix, Linux, and even Windows.
套接字 API 尽管由伯克利人发起,但已被移植到许多平台,包括 Unix、Linux,甚至 Windows。
In his guide he details the very small addition you need to do to get the same API in Windows and *nix systems.
在他的指南中,他详细介绍了在 Windows 和 *nix 系统中获得相同 API 所需要做的非常小的添加。
Once you've learned, wrap it yourself if you're so inclined. Then you can control exactly how "lightweight" you want it.
一旦你学会了,如果你愿意的话,自己把它包起来。然后,您可以准确控制您想要的“轻量级”。
回答by Sam Miller
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。尽管名称如此,但您不会被迫使用异步 I/O。正如您的问题所暗示的那样,您可以使用同步 I/O 和线程。
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.
Boost.Asio 是一个用于网络和低级 I/O 编程的跨平台 C++ 库,它使用现代 C++ 方法为开发人员提供一致的异步模型。
回答by Davis King
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 examplefor instance. Or you can just make a simple iosockstreamif not acting as a server.
如果您真的不喜欢 Boost asio,那么您可能会喜欢dlib 中的套接字支持。它更简单,因为它使用传统的阻塞 IO 和线程,而不是 asio 的异步前摄器模式。例如,它可以很容易地创建一个从 iostreams 读取和写入的线程 TCP 服务器。例如,请参见此示例。或者,如果不充当服务器,您可以制作一个简单的iosockstream。
回答by djaa2807
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/
http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/
回答by Magnus Andermo
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.
看看 ENet http://enet.bespin.org/它是非常轻量级和便携的并且在 UDP 之上工作,并且可选地支持可靠的数据包。它易于使用,API 是低级的,性能开销很小。您可以高度控制内存管理,如果网络是您的瓶颈并且您使用的 malloc/new 实现在多线程下性能不佳,那么这可能会很好。
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.
“最优地”实现你的高级线程并不难,因为有对阻塞接收的可选支持,并且库是一个“库”而不是框架,因此你是决策者而不是库。
回答by kert
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/
老问题,但对于 C++、BSD 风格的同步套接字,这大约是您可以找到的最小行李包装器 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
它确实有例外。您可以制作一个更轻量级的模板库作为仅标头的模板库,并且可以将异常设为可选,但这会稍微改变 API
POCO network classes are quite similar, but do require more dependencies from other parts of the Poco lib
POCO 网络类非常相似,但确实需要 Poco 库其他部分的更多依赖
回答by Ugo Giordano
I'm personally creating my own AsIO wrapper for both TCP and Serial sockets, and I started by reviewing the following tutorial:
我个人正在为 TCP 和串行套接字创建我自己的 AsIO 包装器,我首先查看了以下教程:
https://www.gamedev.net/blogs/blog/950-they-dont-teach-this-stuff-in-school/
https://www.gamedev.net/blogs/blog/950-they-dont-teach-this-stuff-in-school/
and
和
I found the first one very useful and simple to understand.
我发现第一个非常有用且易于理解。
回答by SebastianK
Perhaps you can have a look at http://www.pt-framework.org/
也许你可以看看http://www.pt-framework.org/