UDP Broadcast Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raviskar
    New Member
    • Feb 2009
    • 2

    UDP Broadcast Issue

    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:
    #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(sockfd, SOL_SOCKET, SO_REUSEADDR,
    &reuse, sizeof reuse)) == -1) {
    perror("setsockopt - SO_SOCKET ");
    exit(1);
    }
    if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
    &broadcast, sizeof broadcast)) == -1) {
    perror("setsockopt - SO_SOCKET ");
    exit(1);
    }
    printf("Socket created\n");
    memset(&recvaddr, 0, sizeof recvaddr);
    recvaddr.sin_family = AF_INET;
    recvaddr.sin_port = htons(PORT);
    recvaddr.sin_addr.s_addr = INADDR_ANY;
    if (bind(sockfd, (struct sockaddr*)&recvaddr, 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(sockfd, &set)) {
    perror("FD_ISSET");
    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("recvfrom: '%.*s' at %s\n", numbytes, buf, ctime(&now));
    }
    else
    perror("recvfrom");
    }
    }
    close(sockfd);
    return 0;
    }
    Last edited by JosAH; Feb 5 '09, 10:23 AM. Reason: added [code] ... [/code] tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    If you are using Winsock the fourth paramter to setsockopt() should be a char* and boradcast is an int
    Code:
    if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
        &broadcast, sizeof broadcast)) == -1) {
    see


    however, the open group specify the fourth parameter are a void* - depends on the system you are using

    otherwise nothing obvious!

    Comment

    Working...