C ++ DGRAM套接字获取RECEIVER地址 [英] C++ DGRAM socket get the RECEIVER address
问题描述
在C ++中,
如何获取使用recvfrom接收的UDP数据包的接收方地址。我知道它应该是相同的主机,我收到的数据包,但我需要从接收的数据包中提取,以验证一些东西。我如何做到这一点?
我发现一种方法是:
code> int r = getsockopt(receiver_sock,SOL_IP,SO_ORIGINAL_DST,(struct sockaddr *)& sender_addr,(socklen_t *)& addr_len);`
但我遇到错误:
错误:SO_ORIGINAL_DST未在此范围中声明
我使用适当的标题
#include< sys / types.h>
#include< sys / socket.h>
#include< netinet / in.h>
#include< /usr/src/linux-headers-2.6.32-21/include/linux/netfilter_ipv4.h>
#include< arpa / inet.h>
#include< linux / netfilter.h>
使用netfilter_ipv4给出其他错误,如INT_MIN未声明。但是,我认为错误是更根本的,而不是包含正确的标题。
请帮助。
在Linux上,您要使用 IP_PKTINFO
选项,请参阅 ip(7)
, http://www.kernel.org/doc/man-pages/online/pages/man2/sendmsg.2.html =nofollow> recvmsg(2)
呼叫。
Stevens 有这样做的例子,但使用在Linux上不可用的 IP_RECVDSTADDR
和 IP_RECVIF
选项。 / p>
In C++,
how can I get the receiver address of the UDP packet which I have received using recvfrom. I know that it should be the same host on which I am receiving the packet, but I need to extract it from the received packet, in order to verify something. How can I do this?
I found that one way of doing this is:
int r = getsockopt(receiver_sock, SOL_IP, SO_ORIGINAL_DST, (struct sockaddr *) &sender_addr, (socklen_t *)&addr_len);`
But I get the error:
error: ‘SO_ORIGINAL_DST’ was not declared in this scope
I am using the appropriate headers
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include </usr/src/linux-headers-2.6.32-21/include/linux/netfilter_ipv4.h>
#include <arpa/inet.h>
#include <linux/netfilter.h>
Using netfilter_ipv4 gives other errors like INT_MIN not declared. However, I think the mistake is something more fundamental rather than inclusion of proper header.
Please help.
On Linux you want to use IP_PKTINFO
option, see ip(7)
, and the recvmsg(2)
call.
Stevens has examples of doing this but with IP_RECVDSTADDR
and IP_RECVIF
options that are not available on Linux.
这篇关于C ++ DGRAM套接字获取RECEIVER地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!