获取接收到的UDP报文的目的地址 [英] Get destination address of a received UDP packet

查看:156
本文介绍了获取接收到的UDP报文的目的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接收到UDP数据包后,我需要用发送者用来发送数据包的地址来响应发件人.

通过recvfrom调用,我可以获取发送方的地址,但是如何获取接收到的数据包的目标地址,该地址应与本地主机接口之一的地址匹配?

解决方案

您可以使用setsockopt设置IP_PKTINFO选项,然后使用recvmsg并在结构msghdr的msg_control成员中获取in_pktinfo结构. in_pktinfo有一个包含数据包目的地址的字段.

请参阅: http://www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/,我在其中找到了更多详细信息的答案.

Upon receiving a UDP packet, I need to respond to the sender with the address he used to send the packet to which I'm replying.

The recvfrom call lets me get the address of the sender, but how do I get the destination address of the received packet, which should match the address of one of the local host's interfaces?

解决方案

You set the IP_PKTINFO option using setsockopt and then use recvmsg and get a in_pktinfo structure in the msg_control member of struct msghdr. the in_pktinfo has a field with the destination address of the packet.

See: http://www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/ where I found the answer for more details.

这篇关于获取接收到的UDP报文的目的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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