c++ - Asio 如何包含基于 BSD 套接字 API 的低级套接字接口(interface)?

标签 c++ boost boost-asio

来自 Asio 文档:

The Asio library includes a low-level socket interface based on the BSD socket
API, which is widely implemented and supported by extensive literature. It is
also used as the basis for networking APIs in other languages, like Java. This
low-level interface is designed to support the development of efficient and
scalable applications.

那么是不是Asio库重新实现了底层socket接口(interface)呢?还是 Asio 库套接字接口(interface)是 BSD 套接字 API/Winsock 的包装器,具有很多功能?如果重新实现的话,我希望看到很多内核代码。

最佳答案

的确,套接字实现抽象了底层操作系统实现。 WinInet 和 linux 套接字都类似于 BSD 套接字。

“低级”API 将在 asio::ip::basic_socket<...>::read_some 中和 ...::write_some与 BSD 级别 API 处于同一级别。

事实上,服务实现中继到底层调用,抽象出阻塞与非阻塞 IO 的(特定于实现的)细节并等待就绪的套接字(不需要您使用线程)。

可以通过使用 native_handle() 仍然使用带有原始套接字句柄的 native API。来自服务对象内部的成员函数(例如 https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/reference/basic_socket/native_handle.html )。

Be careful not to break implementation invariants, though. E.g. calling ::close on such a handle, or changing the blocking mode will invite spurious bugs and deadlocks.

关于c++ - Asio 如何包含基于 BSD 套接字 API 的低级套接字接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51718654/

相关文章:

c++ - 不是 lambda 函数中的常量表达式

boost io_service.accept : Get URI called

C++,boost asio,慢读

c++ - 使用哪种动态容器类型?

c++ - Boost序列化基类

python - bjam `Unable to find file or target named ' libboost_python'`

c++ - 在 Boost 1.59 log v2 中添加附加标志后文件轮换问题

C++ boost :asio what happens to server class when session class is deleted

c++ - 这个 add_node 函数会导致内存问题吗?

c++ - 字符串转 float ,C++实现