如何找到使用boost :: ASIO一个UDP数据包的目的地址? [英] How to find the destination address of a UDP packet using boost::asio?

查看:1006
本文介绍了如何找到使用boost :: ASIO一个UDP数据包的目的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个对等通信网络用于在工业环境中使用超过一个局域网。一些消息都只是异步的,并且不要求响应。其他人则请求 - 响应。请求消息(和异步消息)被发送到多播组,以及答复请求发送单播。每个端点,因此,接收发送到组播组的UDP数据包,并且还使用普通的单播收到刚刚发送给它的消息。

I'm developing a peer-to-peer communications network for use over a LAN in an industrial environment. Some messages are are just asynchronous, and don't require a response. Others are request-response. The request messages (and the async messages) are sent to a multicast group, and the replies to requests are sent unicast. Each endpoint, therefore, receives UDP packets that are sent to the multicast group, and also receives messages that are just sent to it using plain unicast.

到目前为止,它的正常工作,但似乎并没有在升压:: ASIO任何方法,找出接收UDP数据包(使用socket.async_receive_from)的目的地址 - 它是否发送到组播组或实际接口。我可以使用该消息的内容来推断是否被发送组播或单播,但它会很高兴能够同时检查目的地址。

So far it's working fine, but there doesn't seem to be any way in boost::asio to find out the destination address of a received UDP packet (using socket.async_receive_from) - whether it was sent to the multicast group or the actual interface. I can use the contents of the message to infer whether it was sent multicast or unicast, but it would be nice to be able to also check the destination address.

目前,我们正在使用Windows 7,但在未来将过渡到Linux。

We are currently using Windows 7, but will be transitioning to Linux in the future.

有没有办法找到一个UDP数据包的目的地址收到使用boost :: ASIO?

Is there a way to find the destination address of a UDP packet received using boost::asio?

推荐答案

可惜,这是不可能的提振:: ASIO,通常是不是做路,当你试图访问的传输层在的应用层

Unfortunately this is not possible with boost::asio, and usually is not "the way to do" it, as you try to access Transport Layer information at the Application Layer.

所以你基本上有两种选择:

So you basically have two options:

a)写出非便携式系统code例如与IP_PKTINFO或SO_BINDTODEVICE在Linux上。例如:code能升压ASIO邮件列表的这里

a) Write non-portable system code with for example IP_PKTINFO or SO_BINDTODEVICE on Linux. Example Code can be found on the boost asio mailing list here

二)使用两个不同的插座,一个用于多播和一个用于单播。因此,你需要在每个插槽上指定比0.0.0.0以外的listen_address。

b) use two distinct sockets, one for the multicast and one for the unicast. You therefore need to specify a listen_address other than "0.0.0.0" on each socket.

udp::endpoint(address_v4::from_string("239.192.152.143"), 6771)

这所以这个问题也可能会有所帮助:<一href=\"http://stackoverflow.com/questions/2065495/using-a-specific-network-interface-for-a-socket-in-windows\">Using在Windows中一个插座一个特定的网络接口

This Question on SO might also be helpful: Using a specific network interface for a socket in windows

这篇关于如何找到使用boost :: ASIO一个UDP数据包的目的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆