如何使用 Boost ASIO 在 UDP 服务器中传递源 IP
我需要获取我使用 boost ASIO 编写的 UDP 服务器中数据报的源 IP。 在示例 udp 数据报服务器中,该行:
注意:我当前的代码与 boost asio 文档中现有的 udp 异步服务器示例相同。
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
正在传递错误和 bytes_transferred。我还需要消息的地址。
现在我知道我可以直接使用 sender_endpoint_.address().to_string() ,但我想将其作为参数传递。
所以我尝试将 sender_endpoint_.address() 作为参数放置。 即
socket_.async_receive_from(
boost::asio::buffer(data_, MAX_LENGTH), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
sender_endpoint_.address()));
但是从handle_receive_from读取时结果是0.0.0.0。 但是,如果我只是访问 handle_receive_from 中的 sender_endpoint_.address() 我得到的 127.0.0.1 是正确的。
如何正确通过这个?我需要这个,因为将有多个线程调用 io_service.run(),因此来自另一个源的数据包可能会覆盖 sender_endpoint,并且我需要能够回复该源数据包。
I need to get the source IP for a datagram in a UDP server I'm writing using boost ASIO.
In the example udp datagram server the line:
Note: my current code is identical to the existing udp async server example in the boost asio documentation.
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
Is passing error and bytes_transferred. I also need the address of the message.
Now I know I can use sender_endpoint_.address().to_string() directly, but I
I would like to pass this in as a parameter.
So I tried placing sender_endpoint_.address() in as a parameter.
i.e.
socket_.async_receive_from(
boost::asio::buffer(data_, MAX_LENGTH), sender_endpoint_,
boost::bind(&server::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
sender_endpoint_.address()));
But the result is 0.0.0.0 when read from handle_receive_from.
However, if I simply access the sender_endpoint_.address() inside handle_receive_from
I get 127.0.0.1 which is correct.
How do pass this correctly? I need this because there are going to be multiple threads calling io_service.run() so a packet from another source may overwrite sender_endpoint and I need to be able to reply to that source packet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,如果您在设置另一个异步处理程序之前访问处理程序中的 sender_endpoint_.address() ,那么它会很好并且不会被覆盖。您确实在处理程序中再次调用 async_receive_from ,对吧,准备好接收另一个数据包?
在第二个代码块中,您尝试将 sender_endpoint_.address() 作为参数,当您设置处理程序时,会调用 sender_endpoint_.address() ,而不是在收到数据包时由处理程序执行。这就是为什么它总是 0.0.0.0。
I think that if you access sender_endpoint_.address() in your handler before you set up another async handler, it will be fine and not overridden. You do call async_receive_from again in your handler, right, to be ready to receive another packet?
In your second code block, where you try sender_endpoint_.address() as a parameter, sender_endpoint_.address() is getting called when you set up the handler, not being executed by the handler when a packet is received. That's why it'll always be 0.0.0.0.