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

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

问题描述

收到 UDP 数据包后,我需要用他用来发送数据包的地址回复发件人,我正在回复.

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

解决方案

您使用setsockopt 设置IP_PKTINFO 选项,然后使用recvmsg 并在struct 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天全站免登陆