I use BSD sockets in Ubuntu 9.10 to send UDP packets in broadcast mode with the following code:
sock_fd = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
receiver_addr.sin_family = PF_INET;
receiver_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
inet_aton("169.254.255.255",&receiver_addr.sin_addr);
receiver_addr.sin_port = htons(port);
int broadcast = 1;
if (setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &broadcast,
sizeof broadcast) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
ret=sendto(sock_fd, packet, size, 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));
Note that this is not all code; it should only have an idea.
The program sends all data using INADDR_BROADCAST if I am connected to a wireless infrastructure network. However, if my laptop is connected to an ad-hoc network, it can receive all the data, but not send it. I solved the problem using the broadcast address 169.254.255.255, but I would like to know what is going on.
Thank you in advance!
source
share