[英]UDP broadcast in C
When I broadcast a message by the code below, the server on the host machine also receive the message, how can I prevent host machine to receive the message that it sends? 当我通过下面的代码广播消息时,主机上的服务器也会收到消息,如何防止主机接收它发送的消息?
Is it possible to change something on the code or would it be better to use something like if (strcmp(hostIP == IP_of_the_package) == 0) { <discard the msg>}
? 是否可以在代码上更改某些内容,或者更好地使用
if (strcmp(hostIP == IP_of_the_package) == 0) { <discard the msg>}
? the host machine gets IP from DHCP, how can I define hostIP as a variable
and How can I extract IP addr of the packet
? 主机从DHCP获取IP,如何将
hostIP as a variable
定义hostIP as a variable
如何提取IP addr of the packet
?
void boardcast_msg(char *mess){
int sock;
struct sockaddr_in broadcastAddr;
char *broadcastIP;
unsigned short broadcastPort;
char *sendString;
int broadcastPermission;
int sendStringLen;
broadcastIP = "255.255.255.255";
broadcastPort = 33333;
sendString = mess; /* string to broadcast */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
fprintf(stderr, "socket error");
exit(1);
}
broadcastPermission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
fprintf(stderr, "setsockopt error");
exit(1);
}
/* Construct local address structure */
memset(&broadcastAddr, 0, sizeof(broadcastAddr));
broadcastAddr.sin_family = AF_INET;
broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);
broadcastAddr.sin_port = htons(broadcastPort);
sendStringLen = strlen(sendString);
/* Broadcast sendString in datagram to clients */
if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *)&broadcastAddr, sizeof(broadcastAddr)) != sendStringLen){
fprintf(stderr, "sendto error");
exit(1);
}
}
Disable loopback so you do not receive your own datagrams: 禁用环回,因此您不会收到自己的数据报:
char loopch=0;
if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP,
(char *)&loopch, sizeof(loopch)) < 0) {
perror("setting IP_MULTICAST_LOOP:");
close(sd);
exit(1);
}
from: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzab6%2Frzab6x1multicast.htm 来自: http : //publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic =%2Frzab6%2Frzab6x1multicast.htm
You are on the right track, you need to see if a received package is from your self, and then discard it. 您走在正确的轨道上,您需要查看收到的包裹是否来自您自己,然后将其丢弃。
The simplest way to get the local hosts address is to use gethostname
and gethostbyname
. 获取本地主机地址的最简单方法是使用
gethostname
和gethostbyname
。 These will however not work well if your host has multiple hostnames or IP addresses. 但是,如果您的主机有多个主机名或IP地址,这些将无法正常工作。 Search SO (or Google) for how to get all possible network addresses.
搜索SO(或Google)以了解如何获取所有可能的网络地址。
your program's broadcast destination port should be blocked on your machine to avoid receiving self broadcasts. 您的程序的广播目标端口应在您的计算机上被阻止,以避免接收自播。
in your iptables
you can drop the ( TCP
or UDP
as required ) packets received on that port. 在您的
iptables
您可以删除在该端口上收到的(根据需要的TCP
或UDP
)数据包。
recvfrom
will give you an address. recvfrom
会给你一个地址。 Use that. 用那个。
As for what addresses to match against, on many systems (probably the ones you care about) you can get local IP addresses with getifaddrs
. 至于要匹配的地址,在许多系统(可能是您关心的系统)上,您可以使用
getifaddrs
获取本地IP地址。 However this is not in POSIX. 但是这不是POSIX。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.