I have several embedded systems running Linux 2.6.33.9rt. They communicate using udp broadcast over wifi. The communication works quite well but after a few minutes some clients stop sending packets. They still receive them, though.
I was able to reproduce the problem using the following example code. The code runs for several minutes spamming broadcast messages and then it just stops. The program is still running an responding. The udp messages just don't show up on the network anymore.
Once the program stopped sending udp the only way to get it started again is by disconnecting and reconnecting the network.
Any ideas what might cause this kind of behavior?
int main(int argc, char**argv) {
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("255.255.255.255");
addr.sin_port = htons(atoi(argv[1]));
int num = atoi(argv[2]);
int s;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("unable to open socket");
exit(1);
}
static int broadcast = 1;
if(setsockopt(s,
SOL_SOCKET,
SO_BROADCAST,
&broadcast,
sizeof(broadcast)) < 0)
{
perror("unable to enable broadcast");
}
for (int i = 0; i < num; ++i)
{
char data[1400];
if (sendto(s, data, 1400, 0,(struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("send error");
exit(1);
}
}
close(s);
return 0;
}
Binding the socket to a device specific broadcast address, in my case 192.168.5.255, fixes the bug. I have no idea why this happens but it works.