如何使用 Boost ASIO 在 UDP 服务器中传递源 IP

发布于 2024-09-12 02:25:32 字数 1142 浏览 2 评论 0原文

我需要获取我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-09-19 02:25:32

我认为,如果您在设置另一个异步处理程序之前访问处理程序中的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文