Hi,
I have a server which opens 2 UDP sockets, say A and B. I am using socket A for just listening on "0.0.0.0" address. I want to receive broadcast packets on this. I have set this socket using SO_BROADCAST. Similarly second socket B is also set with SO_BROADCAST option. Both sockets are set for non-blocking I/O using fcntl() function. I am using socket B for receiveing the packets on a Unicast address by binding it to a specific address. I am also using it to send unicast and broadcast replies back to the clients which communicates with the unicast address, instead of broadcast.
Now my problem is I have added these to sockets in a fd_set and I am running select() in the set, however, my select() call never returns successfully. It always returns with 0. No error either.
I see that both my sockets are bound.
netstat -na | grep :67
udp 0 0 192.168.0.20:67 0.0.0.0:*
udp 8960 0 0.0.0.0:67 0.0.0.0:*
But when client sends data, select() never returns a successfully.
Whats wrong here? can someone help me figure out?
Here is a snippet of the code
Code:
fcntl(socksrusb, F_SETFL, O_NONBLOCK); /* Socket B above */
fcntl(sockrb, F_SETFL, O_NONBLOCK); /* Socket A above */
FD_SET(sockrb,&read_set);
FD_SET(socksrusb,&read_set);
sin_size = sizeof(struct sockaddr);
for (;;)
{
nb = select(fd_max+1,&read_set,NULL,NULL,&wait);
if (nb < 0)
{
perror("select");
close(socksrusb);
close(sockrb);
exit(1);
}
if(nb > 0) printf("nb: %d\n",nb);
if (nb)
{
printf("pkt received\n");
if (FD_ISSET(sockrb,&read_set))
{
/* Packet received on a Broadcast Socket */
if ((bytes_rcvd = recvfrom(sockrb,(dhcppkt *)&dpkt,sizeof(dpkt),0,
(struct sockaddr *)&caddr,&sin_size)) < 0)
{
perror("recvfrom:sockrb");
continue;
} else {
/* Packet Processing code with sendto() in the end*/
} else if (FD_ISSET(socksrusb,&read_set))
{
/* Packet received on a Unicast/Broadcast Socket */
if ((bytes_rcvd = recvfrom(socksrusb,(dhcppkt *)&dpkt,
sizeof(dpkt),0,(struct sockaddr *)&caddr,
&sin_size)) < 0)
{
perror("recvfrom:socksrusb");
continue;
} else {
/* Packet Processing code with sendto() in the end*/
}
}
}
Thanks,