title: Timeout on recvfrom ( winsock2 & udp )
body: <p>I'm trying to implement a <code>timeout</code> on the <code>recvfrom()</code> function.</p>
  
  <p>To do so, I use the <code>select()</code> function. I take the code from the great Internet, but don't know why, my program crash when I use it.
  I launch this server in athread if that coudl help.
  Here's what I try to do :</p>
  
  <pre><code>...
          // Setup timeval variable
          timeval timeout;
          timeout.tv_sec = 5;
          timeout.tv_usec = 0;
  
          // Setup fd_set structure
          fd_set fds;
          FD_ZERO(&amp;fds);
          FD_SET(id_de_la_socket, &amp;fds);
  
          // Return value:
          // -1: error occurred
          // 0: timed out
          // &gt; 0: data ready to be read
          int retval = select(id_de_la_socket+1, &amp;fds, NULL, NULL, &amp;timeout);
          if(retval == -1){
              printf("Error");
              return NULL;
          }
          else if(retval == 0){
              printf("Timeout");
              return NULL;
          }
          else{
              nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&amp;information_sur_la_source,&amp;tempo);
  ...
  </code></pre>
  
  <p>Before trying to implement the <code>timeout</code>, everything works fine, my <code>recvfrom()</code> was in block mode. So I think the problem come from this code that I add. Maybe it's a parameter of a function that I don't understand well.</p>
  
  <p>Thanks for your help.</p>
  
  <p>EDIT : Full code :</p>
  
  <pre><code>#include "serveur.h"
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  serveur::serveur()
  {
  }
  
  StructureSupervision::T_StructureSupervision* serveur::receiveDataUDP(){
      WSADATA initialisation_win32;
      int erreur; 
      int tempo; 
      int nombre_de_caractere; 
      char buffer[65535]; 
      SOCKET id_de_la_socket; 
      SOCKADDR_IN information_sur_la_source; 
  
      erreur=WSAStartup(MAKEWORD(2,2),&amp;initialisation_win32);
      if (erreur!=0)
          printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
      else
          printf("\nWSAStartup  : OK");
  
  
      id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
      if (id_de_la_socket==INVALID_SOCKET)
          printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d",WSAGetLastError());
      else
          printf("\nsocket      : OK");
  
  
      information_sur_la_source.sin_family=AF_INET;
      information_sur_la_source.sin_addr.s_addr = inet_addr("10.100.13.129"); // Ecoute sur toutes les IP locales
      information_sur_la_source.sin_port=htons(4000); // Ecoute sur le port 4000
      erreur=bind(id_de_la_socket,(struct sockaddr*)&amp;information_sur_la_source,sizeof(information_sur_la_source));
      if (erreur!=0)
          printf("\nDesole, je ne peux pas ecouter ce port : %d %d",erreur,WSAGetLastError());
      else
          printf("\nbind        : OK \n");
  
  
      tempo=sizeof(information_sur_la_source); // Passe par une variable afin d'utiliser un pointeur
  
      // Setup timeval variable
      timeval timeout;
      timeout.tv_sec = 5;
      timeout.tv_usec = 0;
      // Setup fd_set structure
      fd_set fds;
      FD_ZERO(&amp;fds);
      FD_SET(id_de_la_socket, &amp;fds);
      // Return value:
      // -1: error occurred
      // 0: timed out
      // &gt; 0: data ready to be read
      ULONG NonBlock = 1; ioctlsocket(id_de_la_socket, FIONBIO, &amp;NonBlock);
      int retval = select(id_de_la_socket+1, &amp;fds, NULL, NULL, &amp;timeout);
      if(retval == -1){
          printf("Error");
          return NULL;
      }
      else if(retval == 0){
          printf("Timeout");
          return NULL;
      }
      else{
      nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,/*MSG_PARTIAL*/0,(struct sockaddr*)&amp;information_sur_la_source,&amp;tempo);
      buffer[nombre_de_caractere]=0; // Permet de fermer le tableau apr�s le contenu des data, car la fonction recvfrom ne le fait pas
      //printf("\nVoici les donnees : %s",buffer);
      StructureSupervision::T_StructureSupervision *structureReception = (StructureSupervision::T_StructureSupervision *) buffer;
      std::cout &lt;&lt; "Voici le numero de Statut Ground Flight : " &lt;&lt; structureReception-&gt;SystemData._statutGroundFlight;
  
  
      erreur=closesocket(id_de_la_socket);
      if (erreur!=0)
          printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %d %d",erreur,WSAGetLastError());
      else
          printf("\nclosesocket : OK");
  
      // ********************************************************
      // Quitte proprement le winsock ouvert avec la commande WSAStartup
      // ********************************************************
      erreur=WSACleanup(); // A appeler autant de fois qu'il a �t� ouvert.
      if (erreur!=0)
          printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
      else
          printf("\nWSACleanup  : OK");
  
      return structureReception;
      }
  }
  </code></pre>