最近在写一个udp通信的程序,由于需要先发送一个广播包,然后等待接收一个回复。

最开始发包(sendto)的时候,一切顺利,但是后来死活不能接收广播包。recvfrom 要么超时,要么一直堵塞着(blocked),耽搁了一天的时间,后来仔细谷歌,才发现是因为内核使用了反向过滤的技术。只要关闭就行了。怎么关闭呢:

以root身份运行:

sysctl -w  net.ipv4.conf.all.rp_filter=0

sysctl -w  net.ipv4.conf.eth0.rp_filter=0

其实该技术打开时,不只是广播包会被内核丢弃,组播包也会被丢弃。现付上一个参考连接:https://www.centos.org/forums/viewtopic.php?t=7775

由于浪费了我不少时间,现将此情况记录起来,希望此文能帮助到你。

if (recvfrom(sock, (void*)dhcp_reply, sizeof(dhcp_reply), 0, (struct sockaddr*)&servAddr, (socklen_t*)&revedAddrLen) == -1) {
			fprintf(stderr, "未收到 DHCP Offer消息:%s\n请尝试用root用户运行 sysctl -w  net.ipv4.conf.all.rp_filter=0\nsysctl -w  net.ipv4.conf.eth1.rp_filter=0", strerror(errno));
			exit(-1);
		}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据