c++ - Boost asio udp socket发送到不同的IP地址
问题描述
我有一台 udp 服务器从多个远程客户端接收消息。当它收到一条消息时,我复制端点并在每个客户端正在侦听的端口 5000 上的相同 IP 地址上回复客户端。
我尝试了多种调试策略,并且在我发送回复消息之前打印端点会给我正确的 IP 地址和端口。
发件人:
std::cout << udp_remote_endpoint.address().to_string();
std::string str(packet.begin(), packet.end());
std::cout << str << std::endl;
io_service.post(
[this, packet]()
{
udp_socket.async_send_to(
boost::asio::buffer(packet),
udp_remote_endpoint,
boost::bind(
&uds::handle_write,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
);
在接收器上,我得到了 udp_remote_endpoint,在发送之前,我设置了套接字端点:
new_addr.endpoint = socket.get_udp_remote_endpoint();
new_addr.endpoint.port(5000);
socket.set_udp_remote_endpoint(new_addr.endpoint);
例如,这个输出:
192.168.1.131K-131-1559147491761155
实际上是发送到IP 192.168.1.130。消息内容正确“K-131-1559147491761155”
解决方案
解决了!
我删除了io_service.post
它,它起作用了!
`std::cout << udp_remote_endpoint.address().to_string();
std::string str(packet.begin(), packet.end());
std::cout << str << std::endl;
//io_service.post(
// [this, packet]()
// {
udp_socket.async_send_to(
boost::asio::buffer(packet),
udp_remote_endpoint,
boost::bind(
&uds::handle_write,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
// }
//);`
推荐阅读
- mysql - Mysql 列数成倍增加
- r - 基于先前输入的带有 observeEvent 更新的闪亮模块
- javascript - datepicker 模板和我的自定义 javascript 和 css 的问题
- python - Python datetime 中 %z 的限制是什么?
- python - 如何在 Python 中将 AdaptiveCard 添加到 QnA Bot
- angular - 文件上传问题
- php - 无法运行 composer update 缺少 mbstring
- hbase - HBase WAL 复制 - 是否支持从具有 HBase 1.1.2 的集群到具有 HBase 2.0.2 的集群的 WAL 复制?
- php - Laravel - 如何使用表单将变量从视图传递到控制器
- lua - 我用seek("cur",-1)从头到尾遍历一个txt文件,但是当它面对一个新行时,会有两个“\n”在连续的位置,为什么?