我在我的UWP应用程序中使用Boost Asio套接字,检查资源监视器,我发现当我创建Boost Asio套接字时,它总是像这样创建两个Loopback。
经过多次调试,我发现它总是在socket_select_interrupter.ipp中为UWP应用创建Loopback
void socket_select_interrupter::open_descriptors()
{
boost::system::error_code ec;
socket_holder acceptor(socket_ops::socket(
AF_INET, SOCK_STREAM, IPPROTO_TCP, ec));
if (acceptor.get() == invalid_socket)
boost::asio::detail::throw_error(ec, "socket_select_interrupter");
int opt = 1;
socket_ops::state_type acceptor_state = 0;
socket_ops::setsockopt(acceptor.get(), acceptor_state,
SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt), ec);
using namespace std; // For memset.
sockaddr_in4_type addr;
std::size_t addr_len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = socket_ops::host_to_network_long(INADDR_LOOPBACK);
addr.sin_port = 0;
if (socket_ops::bind(acceptor.get(), &addr,
addr_len, ec) == socket_error_retval)
boost::asio::detail::throw_error(ec, "socket_select_interrupter");
if (socket_ops::getsockname(acceptor.get(), &addr,
&addr_len, ec) == socket_error_retval)
boost::asio::detail::throw_error(ec, "socket_select_interrupter");
// Some broken firewalls on Windows will intermittently cause getsockname to
// return 0.0.0.0 when the socket is actually bound to 127.0.0.1. We
// explicitly specify the target address here to work around this problem.
if (addr.sin_addr.s_addr == socket_ops::host_to_network_long(INADDR_ANY))
addr.sin_addr.s_addr = socket_ops::host_to_network_long(INADDR_LOOPBACK);
在Win32应用程序中,环回不存在,因为Boost Asio使用I/O完成端口(ASIO_HAS_IOCP)
basic_socket.hpp
#if defined(BOOST_ASIO_WINDOWS_RUNTIME)
detail::io_object_impl<
detail::null_socket_service<Protocol>, Executor> impl_;
#elif defined(BOOST_ASIO_HAS_IOCP)
// For Win32 app
detail::io_object_impl<
detail::win_iocp_socket_service<Protocol>, Executor> impl_;
#elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
detail::io_object_impl<
detail::io_uring_socket_service<Protocol>, Executor> impl_;
#else
// For UWP app
detail::io_object_impl<
detail::reactive_socket_service<Protocol>, Executor> impl_;
#endif
我有一个合作伙伴,不希望环回存在于我的应用程序,所以我尝试了一切禁用或删除环回,但没有希望,我没有找到任何Boost Asio的文档。
*socket_select_interrupter的用途是什么?
- 为什么Boost Asio socket总是在UWP应用程序中创建Loopback?
- 有什么方法可以禁用或删除环回?
1条答案
按热度按时间ibps3vxo1#
你遇到了一些实施细节。
您可以使用此处记录的宏检查IOCP是否已启用/可以启用:https://www.boost.org/doc/libs/1_82_0/doc/html/boost_asio/using.html#boost_asio.using.macros
此外,这里还有与平台相关的实现注解:https://www.boost.org/doc/libs/1_82_0/doc/html/boost_asio/overview/implementation.html#boost_asio.overview.implementation.windows_nt__2000__xp__2003__vista__7__8__10_and_11
具体来说,一些实现细节取决于特定功能的使用(如DNS解析)。你也许可以避开它们。除了Winsock,UWP推荐了许多不同的API堆栈https://learn.microsoft.com/en-us/windows/uwp/networking/which-networking-technology