gpt4 book ai didi

c++ - EAGAIN 上 recv()

转载 作者:搜寻专家 更新时间:2023-10-31 00:46:45 25 4
gpt4 key购买 nike

我实现了一个套接字客户端,通过 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;

}

会不会是超时问题?还是堆栈问题?我该如何解决?

在此先感谢您的帮助。最好的问候。

克里斯蒂娜

最佳答案

EAGAIN 表示在非阻塞套接字上没有可供读取的数据。因此,您应该再次运行 recv 调用。

您实际上没有发布足够的代码来表明存在编程错误,但我可以问一下,当您检测到连接已关闭时,您是否也关闭了您的终端,然后再重新建立一切?

关于c++ - EAGAIN 上 recv(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com