gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:35:03 24 4
gpt4 key购买 nike

来自 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com