udp - 如何使用 boost::asio 在 UDP 套接字上断开连接

标签 udp boost-asio

结合 boost docs 中的信息和 manual for connect function我认为 asio::udp::socket::connect 函数用于设置默认发送数据报的地址,以及接收数据报的唯一地址。

这很好用,但手册还指出我应该能够解除关联(断开连接),这就是我在做的时候遇到的问题。 IE。我希望套接字再次开始从任何地址接收数据包。

手册中进一步写道:

Connectionless sockets may dissolve the association by connecting to an
address with the sa_family member of sockaddr set to AF_UNSPEC.

我尝试过这段代码:

asio::udp::endpoint unspecified_endpoint;
assert(unspecified_endpoint.address().is_unspecified()); // OK
socket.connect(unspecified_endpoint);

但这并没有帮助。


编辑:通过采用client创建了一个测试用例和 server来自 boost::asio 页面的示例。在客户端代码中,我已将传出端口号从随机更改为 5192:

udp::socket s(io_service, udp::endpoint(udp::v4(), 5192));

我更改了服务器代码中的函数server:

void server(boost::asio::io_service& io_service, short port)
{
  udp::socket sock(io_service, udp::endpoint(udp::v4(), port));

#define CASE 3

#if CASE == 1
  sock.connect(udp::endpoint());
#elif CASE == 2
  sock.connect(udp::endpoint(ip::address::from_string("127.0.0.1"), 5193));
  sock.connect(udp::endpoint());
#elif CASE == 3
  sock.connect(udp::endpoint(ip::address::from_string("192.168.1.3"), 5192));
  sock.connect(udp::endpoint());
#endif

  for (;;)
  {
    char data[max_length];
    udp::endpoint sender_endpoint;
    size_t length = sock.receive_from(
    boost::asio::buffer(data, max_length), sender_endpoint);
    sock.send_to(boost::asio::buffer(data, length), sender_endpoint);
  }
}

情况 1 和 2 似乎有效,但在情况 3 中客户端不会收到回复。完整的来源可以找到here .

最佳答案

正如评论中提到的,对 sock.connect(udp::endpoint()); 的调用转换为 connect(6, {sa_family=AF_INET, sin_port=htons(0 ), sin_addr=inet_addr("0.0.0.0")}, 16) = 0 这与使用 AF_UNSPECconnect 不同,并且不' t 完全断开 UDP 套接字(似乎只是重置对等端口号)。

我认为没有办法纯粹使用 boost asio 来实现你想要的,但是你可以使用 native POSIX 调用来断开套接字,例如

struct sockaddr unspec_addr = { AF_UNSPEC };
::connect(sock.native_handle(), &unspec_addr, sizeof(unspec_addr));

调试注意事项:使用strace运行程序,然后使用netstat -nu列出UDP套接字。

关于udp - 如何使用 boost::asio 在 UDP 套接字上断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13945422/

相关文章:

java - 实现一个持续向邻居广播的简单 UDP 网络

c# - 使用 char 数组编码异常

检查 UDP 端口是否已在 C 中打开

c++ - boost::asio UDP "gather"操作

c++ - 如何等待 asio 处理程序?

python - 如何通过互联网在两台计算机之间建立 UDP 连接

html - 在 html 视频对象中播放 udp 流

c++ - Web服务器中proactor模式和同步模型的区别

c++ - 澄清 `boost::bind`和 `this`的使用

c++ - 为什么在这种情况下 boost 不创建线程 (C2248) 以及如何创建它?