Problem receiving broadcast messages

Latest response

Hi, My application currently succesfully receives UDP packets from a remote host when the host specifically sends data to my IP address. However, I now have a host which is broadcasting, and my application never receives any data. However, if I use nc ["nc -l -u 15232"], I get data. So why wont my program? Here is my code, stripped of unneccessary waffle...

 udpSocket = socket(PF_INET,SOCK_DGRAM,0); ifr.ifr_addr.sa_family = AF_INET;

strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

ioctl(udpSocket, SIOCGIFADDR, &ifr); if(setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,(void*)&n1,4)){

printf("sockopt error\n"); }

localSockAddr.sin_family = AF_INET; localSockAddr.sin_addr.s_addr = inet_addr("192.168.0.4"); localSockAddr.sin_port=htons((unsigned short) 15232); remoteSockAddr.sin_family = AF_INET; remoteSockAddr.sin_addr.s_addr = inet_addr("192.168.0.56"); remoteSockAddr.sin_port=htons(15232);

if (bind(udpSocket, (SOCKADDR *)&localSockAddr,sizeof(SOCKADDR_IN))) {

error = 1; }

size = sizeof(SOCKADDR_IN);

bytes = recvfrom(udpSocket,rxBuffer,sizeof(rxBuffer),0,(SOCKADDR*)&sockaddr,&size); /* program hangs when remote host broadcasts, gets data OK when remote host sends to "192.168.0.4" */

 

The host is broadcasting on INADDR_BROADCAST which I think is 192.168.0.255. I downloaded the source for nc (0.7.1), built it, and it operates like my program and hangs??? But the internal nc appears to get data!

Here is what netstat thinks for my prog and OS's nc.
[irkn@localhost spg]$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
udp        0      0 192.168.0.4:15232           *:*                                    
udp        0      0 localhost.localdomain:15232 *:*                                    
udp        0      0 *:815                       *:*                                    
udp        0      0 *:818                       *:*                                    
udp        0      0 *:sunrpc                    *:*                                    
udp        0      0 *:ipp                       *:*                                    
[irkn@localhost spg]$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
udp        0      0 192.168.0.4:15232           192.168.0.56:15232          ESTABLISHED
udp        0      0 *:815                       *:*                                    
udp        0      0 *:818                       *:*                                    
udp        0      0 *:sunrpc                    *:*                                    
udp        0      0 *:ipp                       *:*           

Can anyone help?

Responses