关于recv()的EAGAIN

时间:2011-01-24 01:09:47

标签: c++ sockets connection recv

我实现了一个套接字客户端,通过HTTP与RTSP进行通信,从摄像头获取视频。

要建立与摄像头的通信,首先我必须设置HTTP-GET隧道,然后发送RTSP命令。当摄像机失去连接时,程序必须关闭隧道处理程序,完成线程,当进程返回主函数时,它开始通信(启动踏板,依此类推)。

在重新连接时:http-get隧道设置正常,我的意思是,套接字连接并接收“HTTP OK”,因此程序发送RTSP“DESCRIBE”,但recv始终返回EAGAIN错误。我检查了wireshar是否从相机发送了DESCRIBE OK响应,但是recv永远不会得到它。

以下是代码:

   struct sockaddr_in aServer;
  // string myData;
  char *myData=new char [256];
  connection *c=(connection*)vargp;


   memset(&aServer, 0, sizeof aServer);
   aServer.sin_family = AF_INET;

   aServer.sin_addr.s_addr = inet_addr(c->theServer.c_str());
   if (aServer.sin_addr.s_addr == INADDR_NONE)
   {
     struct hostent *hp;

     hp = gethostbyname(c->theServer.c_str());
     if (hp != NULL)
     {
       memcpy(&aServer.sin_addr, hp->h_addr, hp->h_length);
       aServer.sin_family = hp->h_addrtype;    //Protocol family
     }
     else
           cout << "Failed to resolve " << c->theServer.c_str()  << ": " << hstrerror(h_errno) << endl;
   }

   aServer.sin_port = htons(c->thePort);
   c->fd_get = socket(AF_INET, SOCK_STREAM, 0);

   struct timeval timeout;

   timeout.tv_sec = 5;
   timeout.tv_usec = 0;

  setsockopt(c->fd_get, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

   if (c->fd_get < 0){
    cout << "fd_get < 0"  << endl;
      c->bFin=true;
      c->WakeUP();
      }
   if (connect(c->fd_get, (struct sockaddr *) &aServer, sizeof aServer) < 0){
    cout << "connect fd_get < 0"  << endl;
    c->bFin=true;
    c->WakeUP();
   }
   if(!c->bFin){

    sprintf(myData, "GET %s HTTP/1.1\r\n", c->theURI.c_str());
    sprintf(myData, "%sx-sessioncookie: %s\r\nAccept: application/x-rtsp-tunnelled\r\nAuthorization: %s\r\n\r\n", myData,c->theHTTPSessionId.c_str(), c->addAuthorization(c->aGlobalUsername, c->aGlobalPassword).c_str() );

    cout << myData << endl;
    write(c->fd_get, myData, strlen(myData));

    //LISTENING...
    int theLen=1500; //3000;
    int ret=0;
    unsigned char datosRecibidos[3000];

    int flags =fctl(c->fd_get, F_GETFD;
    if((flags & O_NONBLOCK) == O_NONBLOCK){
        fprint(stderr, "yup, its nonblocking");
    }
    else{
        fprint(stderr, "nope, its blocking");
    }


    while (c->bFin==false){

     ret = read(c->fd_get, ReceivedData, theLen);
    // ret= recvfrom(c->fd_get, ReceivedData, theLen, 0,  (struct sockaddr *) 0, (socklen_t*)0);
     if (ret == 0)
     {
      cout << "Server closed connection: 0" << endl;

    }
      else
      if (ret == -1){
     fprintf (stderr, "\n[%d]: %s %d\n", __LINE__, strerror (errno), errno);

     if(errno==107 ||errno==EAGAIN){
      cout << "errno" << endl;
      c->bFin=true;
      c->WakeUP();
      cout << "vuelta wakeUP" << endl;
      break;// empezar de nuevo
     }else{
      cout << "errno" << endl;

     }

    }
    else{
     //cout << (string)ReceivedData[0]<< endl;
     c->ProcessReceivedData(ReceivedData, ret);
     usleep(10);
    }
   }

   close(c->fd_get);
   c->fd_get = -1;

   }

这可能是超时问题吗?还是堆栈问题?我该如何解决?

提前感谢您的帮助。最好的问候。

cristina

3 个答案:

答案 0 :(得分:4)

EAGAIN表示没有可用于在非阻塞套接字上读取的数据。所以,你应该再次运行recv调用。

您实际上没有发布足够的代码来表明存在编程错误,但我可以问一下,当您检测到连接已关闭时,您是否也会在重新建立所有内容之前关闭结束?

答案 1 :(得分:1)

您的套接字是否在O_NONBLOCK模式下打开?您可以这样检查:

int flags = fcntl(fd, F_GETFD);
if ((flags & O_NONBLOCK) == O_NONBLOCK) {
  fprintf(stderr, "Yup, it's nonblocking");
}
else {
  fprintf(stderr, "Nope, it's blocking.");
}

在非阻塞模式下,如果没有任何东西可以接收,则recv将立即返回,并将errno设置为EAGAIN。

答案 2 :(得分:0)

在进行每个套接字读取操作之前,我总是使用poll()或select()。

还要测试NON-Blocking:

    int flags = fcntl(fd, F_GETFL, 0);
    if (flags & O_NONBLOCK) {
     fprintf(stderr, "Yup, it's nonblocking");
   } else {
    fprintf(stderr, "Nope, it's blocking.");
  }