title: EAGAIN on recv()
body: <p>I implented a socket client to communicate to an ip camera with RTSP over HTTP to get teh video from the camera.</p>
  
  <p>To stablished the communication with the camera, first i have to set an HTTP-GET tunnel, then send the RTSP commands. When the camera loses the connection, the program has to close the tunnel handler, finish the thread and when the process return to the main function, it begins the communication (start the treads, and so on).</p>
  
  <p>On the reconnection: the http-get tunnel is set ok, i mean, the socket connects and receives a "HTTP OK", so the program sends a RTSP "DESCRIBE" but the recv always return an EAGAIN error. I check with wireshar that the DESCRIBE OK response is sent from the camera, but the recv never gets it.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>   struct sockaddr_in aServer;
    // string myData;
    char *myData=new char [256];
    connection *c=(connection*)vargp;
  
  
     memset(&amp;aServer, 0, sizeof aServer);
     aServer.sin_family = AF_INET;
  
     aServer.sin_addr.s_addr = inet_addr(c-&gt;theServer.c_str());
     if (aServer.sin_addr.s_addr == INADDR_NONE)
     {
       struct hostent *hp;
  
       hp = gethostbyname(c-&gt;theServer.c_str());
       if (hp != NULL)
       {
         memcpy(&amp;aServer.sin_addr, hp-&gt;h_addr, hp-&gt;h_length);
         aServer.sin_family = hp-&gt;h_addrtype;    //Protocol family
       }
       else
             cout &lt;&lt; "Failed to resolve " &lt;&lt; c-&gt;theServer.c_str()  &lt;&lt; ": " &lt;&lt; hstrerror(h_errno) &lt;&lt; endl;
     }
  
     aServer.sin_port = htons(c-&gt;thePort);
     c-&gt;fd_get = socket(AF_INET, SOCK_STREAM, 0);
  
     struct timeval timeout;
  
     timeout.tv_sec = 5;
     timeout.tv_usec = 0;
  
    setsockopt(c-&gt;fd_get, SOL_SOCKET, SO_RCVTIMEO, &amp;timeout, sizeof(timeout));
  
     if (c-&gt;fd_get &lt; 0){
      cout &lt;&lt; "fd_get &lt; 0"  &lt;&lt; endl;
        c-&gt;bFin=true;
        c-&gt;WakeUP();
        }
     if (connect(c-&gt;fd_get, (struct sockaddr *) &amp;aServer, sizeof aServer) &lt; 0){
      cout &lt;&lt; "connect fd_get &lt; 0"  &lt;&lt; endl;
      c-&gt;bFin=true;
      c-&gt;WakeUP();
     }
     if(!c-&gt;bFin){
  
      sprintf(myData, "GET %s HTTP/1.1\r\n", c-&gt;theURI.c_str());
      sprintf(myData, "%sx-sessioncookie: %s\r\nAccept: application/x-rtsp-tunnelled\r\nAuthorization: %s\r\n\r\n", myData,c-&gt;theHTTPSessionId.c_str(), c-&gt;addAuthorization(c-&gt;aGlobalUsername, c-&gt;aGlobalPassword).c_str() );
  
      cout &lt;&lt; myData &lt;&lt; endl;
      write(c-&gt;fd_get, myData, strlen(myData));
  
      //LISTENING...
      int theLen=1500; //3000;
      int ret=0;
      unsigned char datosRecibidos[3000];
  
      int flags =fctl(c-&gt;fd_get, F_GETFD;
      if((flags &amp; O_NONBLOCK) == O_NONBLOCK){
          fprint(stderr, "yup, its nonblocking");
      }
      else{
          fprint(stderr, "nope, its blocking");
      }
  
  
      while (c-&gt;bFin==false){
  
       ret = read(c-&gt;fd_get, ReceivedData, theLen);
      // ret= recvfrom(c-&gt;fd_get, ReceivedData, theLen, 0,  (struct sockaddr *) 0, (socklen_t*)0);
       if (ret == 0)
       {
        cout &lt;&lt; "Server closed connection: 0" &lt;&lt; endl;
  
      }
        else
        if (ret == -1){
       fprintf (stderr, "\n[%d]: %s %d\n", __LINE__, strerror (errno), errno);
  
       if(errno==107 ||errno==EAGAIN){
        cout &lt;&lt; "errno" &lt;&lt; endl;
        c-&gt;bFin=true;
        c-&gt;WakeUP();
        cout &lt;&lt; "vuelta wakeUP" &lt;&lt; endl;
        break;// empezar de nuevo
       }else{
        cout &lt;&lt; "errno" &lt;&lt; endl;
  
       }
  
      }
      else{
       //cout &lt;&lt; (string)ReceivedData[0]&lt;&lt; endl;
       c-&gt;ProcessReceivedData(ReceivedData, ret);
       usleep(10);
      }
     }
  
     close(c-&gt;fd_get);
     c-&gt;fd_get = -1;
  
     }
  </code></pre>
  
  <p>Could it be a timeout problem? or a stack problem? How can i solve it?</p>
  
  <p>Thanks in advance for your help. Best regards.</p>
  
  <p>cristina </p>