I run the server code below to open a UDP socket. I have two network interfaces on my linux machine and two interfaces are connected to two different networks. I would like the program to listen a specified network (by assigning IP address), therefore I assign an IP address on the UDP socket.
If I use servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
the server is able to receive the broadcast and unicast messages. However, if I define the servaddr.sin_addr.s_addr =inet_addr("10.0.0.6");
then the server can receive the messages originated to 10.0.0.6
but cannot receive the broadcast 10.0.0.255
message (the netmask is /24).
here is the code that broadcasts the message, and the unicast code is here.
Am I assigning the IP address wrong or is the broadcast code wrong?
Server code is:
#define BUFSIZE 512
char *SERVER_IP = "10.0.0.6";
int main() {
int error_count=0, r=0, n=0;
int sockfd = 0;
struct sockaddr_in servaddr, cliaddr ,a ;
socklen_t len; //integer type of width of at least 32 bits
char mesg[1000];
sockfd = socket(AF_INET, SOCK_DGRAM, 0); // for datagram
while(sockfd < 0){ //error handling for socket opening
usleep(500000);
if (++error_count == 20){//10 times itteration
fprintf(stderr, "errno:%s - socket opening error - line 223\n ", strerror(errno));
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
}
error_count = 0;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(33333); //server listens on this port
// servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr =inet_addr(SERVER_IP);
printf("servaddr.sin_addr:%lu\n",servaddr.sin_addr );
printf("a.sin_addr:%lu\n",a.sin_addr );
r = bind(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr));
while(r < 0){ //error handling for socket binding
usleep(500000);
if (++error_count == 20){//10 times itteration
fprintf(stderr, "errno:%s - socket binding error - line 239\n ", strerror(errno));
exit(1);
}
r = bind(sockfd,(struct sockaddr *) &servaddr, sizeof(servaddr));
}
error_count = 0;
while(1){
len = sizeof(cliaddr);
next:
printf("server is listening\n");
n = recvfrom(sockfd, mesg, 1000, 0, (struct sockaddr *) &cliaddr, &len);
printf("line195: packet is received: %s\n", mesg);
if(n < 0){
fprintf(stderr, "recvfrom error occured - line254\n");
n = 0;
goto next;
}
}
return 0;
}
here is my ifconfig -a wlan8
wlan8 Link encap:Ethernet HWaddr 64:70:02:18:1f:b6
inet addr:10.0.0.6 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::6670:2ff:fe18:1fb6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:206 errors:0 dropped:0 overruns:0 frame:0
TX packets:297 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:37857 (37.8 KB) TX bytes:54526 (54.5 KB)