Hi,
I have a problem in receiving UDP broadcast data. We have a simple application which listens for the UDP broadcast data from the external servers. We can receive the UDP broadcast data from one of the servers. For the other external server, our program does not receive any data in the sockets, but we can see the broadcast data in tcpdump capture.
Is there any possible reason on why our sockets cannot receive the UDP broadcast, but the data can be seen in the capture?
I have given our sample application code below. Please help.
Thanks,
Ravi.
[code=c]
#define PORT 6515
int main(int argc, char *argv[])
{
int sockfd;
char buf[30];
struct sockaddr_in sendaddr;
struct sockaddr_in recvaddr;
int numbytes;
socklen_t addr_len;
int broadcast=1;
int reuse=1;
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket" );
exit(1);
}
if ((setsockopt(so ckfd, SOL_SOCKET, SO_REUSEADDR,
&reuse, sizeof reuse)) == -1) {
perror("setsock opt - SO_SOCKET ");
exit(1);
}
if ((setsockopt(so ckfd, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast)) == -1) {
perror("setsock opt - SO_SOCKET ");
exit(1);
}
printf("Socket created\n");
memset(&recvadd r, 0, sizeof recvaddr);
recvaddr.sin_fa mily = AF_INET;
recvaddr.sin_po rt = htons(PORT);
recvaddr.sin_ad dr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&recv addr, sizeof recvaddr) == -1) {
perror("bind");
exit(1);
}
for (;;) {
int n;
fd_set set;
struct timeval time_500ms = { 0, 500*1000 };
FD_ZERO(&set);
FD_SET(sockfd, &set);
n = select(sockfd+1 , &set, NULL, NULL, &time_500ms) ;
if (n < 0) {
perror("select" );
break;
}
else if (n == 0) {
printf("sleep(5 )\n");
sleep(5);
}
else if (!FD_ISSET(sock fd, &set)) {
perror("FD_ISSE T");
break;
}
else {
addr_len = sizeof sendaddr;
if ((numbytes = recvfrom(sockfd , buf, sizeof buf, 0,
(struct sockaddr *)&sendaddr, &addr_len)) > 0)
{
time_t now = time(NULL);
printf("recvfro m: '%.*s' at %s\n", numbytes, buf, ctime(&now));
}
else
perror("recvfro m");
}
}
close(sockfd);
return 0;
}[/code]
I have a problem in receiving UDP broadcast data. We have a simple application which listens for the UDP broadcast data from the external servers. We can receive the UDP broadcast data from one of the servers. For the other external server, our program does not receive any data in the sockets, but we can see the broadcast data in tcpdump capture.
Is there any possible reason on why our sockets cannot receive the UDP broadcast, but the data can be seen in the capture?
I have given our sample application code below. Please help.
Thanks,
Ravi.
[code=c]
#define PORT 6515
int main(int argc, char *argv[])
{
int sockfd;
char buf[30];
struct sockaddr_in sendaddr;
struct sockaddr_in recvaddr;
int numbytes;
socklen_t addr_len;
int broadcast=1;
int reuse=1;
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket" );
exit(1);
}
if ((setsockopt(so ckfd, SOL_SOCKET, SO_REUSEADDR,
&reuse, sizeof reuse)) == -1) {
perror("setsock opt - SO_SOCKET ");
exit(1);
}
if ((setsockopt(so ckfd, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast)) == -1) {
perror("setsock opt - SO_SOCKET ");
exit(1);
}
printf("Socket created\n");
memset(&recvadd r, 0, sizeof recvaddr);
recvaddr.sin_fa mily = AF_INET;
recvaddr.sin_po rt = htons(PORT);
recvaddr.sin_ad dr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&recv addr, sizeof recvaddr) == -1) {
perror("bind");
exit(1);
}
for (;;) {
int n;
fd_set set;
struct timeval time_500ms = { 0, 500*1000 };
FD_ZERO(&set);
FD_SET(sockfd, &set);
n = select(sockfd+1 , &set, NULL, NULL, &time_500ms) ;
if (n < 0) {
perror("select" );
break;
}
else if (n == 0) {
printf("sleep(5 )\n");
sleep(5);
}
else if (!FD_ISSET(sock fd, &set)) {
perror("FD_ISSE T");
break;
}
else {
addr_len = sizeof sendaddr;
if ((numbytes = recvfrom(sockfd , buf, sizeof buf, 0,
(struct sockaddr *)&sendaddr, &addr_len)) > 0)
{
time_t now = time(NULL);
printf("recvfro m: '%.*s' at %s\n", numbytes, buf, ctime(&now));
}
else
perror("recvfro m");
}
}
close(sockfd);
return 0;
}[/code]