title: Multiple UDP sockets using epoll - Not able to receive data
body: <p>Im trying to receive data from multiple UDP sockets using epoll. This is a test program it doesn't have exact number of sockets.( For testing purpose its set as 50.) The below program doesn't receive any data and it gets stuck at epoll_wait(), since i have given -1 it waits indefinetly till it gets data. But doesn't get any.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/epoll.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define MAX_CON (50)
  typedef struct UDPMessage
  {
    unsigned short hdr;
    unsigned short CC1;
    unsigned short CC2;
    unsigned char  data[1472];
    unsigned short CC3;
  } UDPPacket;
  
  static struct epoll_event *events;
  int create_sockets(unsigned int port);
  
  int create_sockets(unsigned int port)
  {
          int sock, flag = 1;
          struct sockaddr_in sock_name;
  
          /* Create a datagram socket*/
          sock = socket(PF_INET, SOCK_DGRAM, 0);
          /* Setting the socket to non blocking*/
          fcntl(sock, F_SETFL, O_NONBLOCK);
  
          if (sock &lt; 0)
          {
                  perror("socket");
                  exit(EXIT_FAILURE);
          }
          /* Set the reuse flag. */
          if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &amp;flag, sizeof(flag)) &lt; 0)
          {
                  perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
                  exit(EXIT_FAILURE);
          }
          /* Give the socket a name. */
          sock_name.sin_family = AF_INET;
          sock_name.sin_port = htons(port);
          sock_name.sin_addr.s_addr = htonl(INADDR_ANY);
          if (bind(sock,(struct sockaddr *) &amp;sock_name, sizeof(sock_name)) &lt; 0)
          {
                  perror("bind");
                  exit(EXIT_FAILURE);
          }
          return sock;
  }
  
  int main(int argc, char *argv[])
  {
          fd_set master;
          fd_set read_fds;
          struct sockaddr_in serveraddr;
          struct sockaddr_in clientaddr;
          int fdmax=0;
          int newfd;
          int nbytes;
          int epfd = -1;
          int res = -1;
          struct epoll_event ev;
          int i=0;
          int index = 0;
          int client_fd = -1;
  
          int SnumOfConnection = 0;
  
          UDPPacket UDPPacket_obj;
          struct sockaddr_in client_sockaddr_in;
          struct sockaddr_in server_sockaddr_in;
  
          int sockfd[12]={0}; 
          int PORTS[12] = { 10011,10012,10013,10021,10022,10023,10031,10032,10033,10041,10042,10043}; //TEST only 12 ports
          int port_index=0;
          int sockaddr_in_length=0;
  
          printf("In main\n");
  
          /* Create sockets with the above list of ports */
          for( port_index=0;port_index&lt;12;port_index++)
          {
                  sockfd[port_index]=create_sockets(PORTS[port_index]);
                  if(sockfd[port_index] &gt; fdmax)
                          fdmax = sockfd[port_index];
          }
          printf("after soc creation\n");
  
          sockaddr_in_length = sizeof(struct sockaddr_in);
  
          events = calloc(MAX_CON, sizeof(struct epoll_event));
          if ((epfd = epoll_create(MAX_CON)) == -1)   /* epoll_create */
          {
                  perror("epoll_create");
                  exit(1);
          }
          ev.events = EPOLLIN;
          ev.data.fd = fdmax;
  
          for( port_index=0;port_index&lt;12;port_index++)
          {
              if (epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd[port_index], &amp;ev) &lt; 0)   /* epoll_ctl */
              {
                    perror("epoll_ctl");
                    exit(1);
              }
           }
  
           while(1)
          {
                  printf("Before epoll_wait\n");
                  res = epoll_wait(epfd, events, MAX_CON, -1);  /* epoll_wait */
                  printf("After epoll_wait\n");
                  client_fd = events[index].data.fd;
  
                  for (index = 0; index &lt; MAX_CON; index++)
                  {
                          printf("start\n");
                          if (events[index].events &amp; EPOLLHUP)
                          {
                                  if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &amp;ev) &lt; 0) {
                                          perror("epoll_ctl");
                                  }
                                  close(client_fd);
                                  break;
                          }
                          printf("before EPOLLIN check\n");
                          if (events[index].events &amp; EPOLLIN)  {
                                  if((nbytes = recvfrom(client_fd,&amp;UDPPacket_obj,sizeof(UDPPacket),0,
                                                                  (struct sockaddr *)&amp;client_sockaddr_in,
                                                                  &amp;sockaddr_in_length)) &lt;=0)
  
                                  {
                                          if(nbytes == 0) {
                                                  printf("socket %d hung up\n", client_fd);
                                          }
                                          else {
                                                  printf("recv() error lol! %d", client_fd);
                                                  perror("");
                                          }
  
                                          if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &amp;ev) &lt; 0) {
                                                  perror("epoll_ctl");
                                          }
                                          //close(client_fd);
                                  }
                                  printf("Data recv=%s\n",UDPPacket_obj.data);
                          }
                  }
          } //end of while
          return 0;
  }
  </code></pre>