multiple UDP sockets with select()

This is a discussion on multiple UDP sockets with select() within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I have a server which opens 2 UDP sockets, say A and B. I am using socket A for ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    134

    multiple UDP sockets with select()

    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,

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Is fd_max set to the correct value? I don't see it getting set to anything in your code. Also, are you sure that the problem is with select? If there really is data to be read, test your code by calling recvfrom(), and see if you can read anything. If you can't then select() is working fine, the problem is elsewhere.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Hi,

    I am setting fd_max when I am creating the 2 sockets. Its iust that I have not shown it in the post. My apologies for that. BTW, my problem is solved now. I passed NULL instead of wait parameter in the select() as suggested to me by someone. This caused select() to wait indefinitely till some data is received on anyone of the two sockets.

    I had this code running with just one socket and recvfrom(). But I needed to add one more socket due to certain functionality limitations. So i migrated the code to use select().

    Thanks anyway for your reponse

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. program structure with select sockets
    By n3v in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-03-2006, 07:34 AM
  4. Sockets, Multiple I/O
    By _Cl0wn_ in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 03:53 PM
  5. Sockets and select()
    By xxx in forum Windows Programming
    Replies: 0
    Last Post: 11-18-2002, 03:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21