title: linux C++ socket select loop
body: <p>I'm having a little trouble with sockets, when looping I'm not receiving data except for the first loop, it's timing out each time. If I close and reopen the socket each loop though I seem to be getting the data correctly. Any ideas as to why?</p>
  
  <p>Example of looping without closing:</p>
  
  <pre><code>int socketHandle = socket(AF_INET,SOCK_DGRAM,0);
  
  sockaddr_in serverAddr;
  
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = inet_addr(/*UDP IP ADDRESS*/);
  serverAddr.sin_port = htons(/*UDP PORT*/);
  
  struct timeval tv;
  fd_set rfds;
  
  FD_ZERO(&amp;rfds);
  FD_SET(socketHandle, &amp;rfds);
  
  tv.tv_usec = 0.0;
  int recVal = 0;
  int sockLen = sizeof(serverAddr);
  bind(socketHandle, (struct sockaddr*)&amp;serverAddr, (socklen_t)sockLen);
  
  bool timePassed = false;
  time_t startListenTime = time(NULL);
  
  tv.tv_sec = maxUpdateTime;
  
  while(true)
  {
      recVal = select(socketHandle + 1, &amp;rfds, NULL, NULL, &amp;tv);
      switch(recVal)
      {
          case(0):
          {
              //Timeout
              break;
          }
          case(-1):
          {
              //Error
               break;
          }
          default:
          {
              /*Packet Data Type*/ pkt;
              if(recvfrom(socketHandle, &amp;pkt, sizeof(/*Packet Data Type*/), 0, (sockaddr*)&amp;serverAddr, (socklen_t*)&amp;sockLen) &lt; 0)
              {
                  //Failed to Recieve Data
                  break;
              }
              else
              {
                  //Recieved Data!!
              }
              break;
          }
      }
  }
  </code></pre>
  
  <p>Example of looping with closing:</p>
  
  <pre><code>while(true)
  {
      int socketHandle = socket(AF_INET,SOCK_DGRAM,0);
  
      sockaddr_in serverAddr;
  
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_addr.s_addr = inet_addr(/*UDP IP ADDRESS*/);
      serverAddr.sin_port = htons(/*UDP PORT*/);
  
      struct timeval tv;
      fd_set rfds;
  
      FD_ZERO(&amp;rfds);
      FD_SET(socketHandle, &amp;rfds);
  
      tv.tv_usec = 0.0;
      int recVal = 0;
      int sockLen = sizeof(serverAddr);
      bind(socketHandle, (struct sockaddr*)&amp;serverAddr, (socklen_t)sockLen);
  
  
      bool timePassed = false;
      time_t startListenTime = time(NULL);
  
      tv.tv_sec = maxUpdateTime;
  
      recVal = select(socketHandle + 1, &amp;rfds, NULL, NULL, &amp;tv);
      switch(recVal)
      {
          case(0):
          {
              //Timeout
              break;
          }
          case(-1):
          {
              //Error
               break;
          }
          default:
          {
              /*Packet Datastructure*/ pkt;
              if(recvfrom(socketHandle, &amp;pkt, sizeof(/*Packet Datastructure*/), 0, (sockaddr*)&amp;serverAddr, (socklen_t*)&amp;sockLen) &lt; 0)
              {
                  //Failed to read packet
                  break;
              }
              else
              {
                  //Read Packet!!
              }
              break;
          }
      }
      close(socketHandle);
  }
  </code></pre>