简体   繁体   English

recv()失败:错误的文件描述符c ++ Linux

[英]recv() failed: Bad file descriptor c++ Linux

I have a problem. 我有个问题。 My program includes 10 TCP Server at the sam etime. 我的程序在同一时间包含10个TCP Server。 As soon as a request from a client is noticed, the appropriate tcp server socket will accept the connection and handle it in a seperate thread. 一旦注意到来自客户端的请求,相应的tcp服务器套接字将接受连接并在单独的线程中进行处理。 I know this is not the most efficient way of solving my actual problem but okay.. 我知道这不是解决我的实际问题的最有效方法,但是可以。

In main I have a for loop that will call a function in an object called Peer which is StartThread() 在main中,我有一个for循环,它将在名为Peer的对象中调用函数,该对象为StartThread()

for (it= ListOfPeers.begin();it!= ListOfPeers.end();it++)
{
    (*it).second->StartThread();
}

Of cause there are some conditions that this loop will be used but i wanted to narrow the code as much down as possible. 当然,在某些情况下会使用此循环,但我想尽可能缩小代码范围。

The function StartThread will be called in each peer object: 函数StartThread将在每个对等对象中调用:

    void StartThread()
{
    pthread_t threadDoEvent;
    pthread_create( &threadDoEvent, NULL, &DoEvent_helper,this);
    pthread_detach(threadDoEvent);
}

void *DoEvent_helper( void *ptr ) // Helper to implement thread
{
    return ((Peer *)ptr)->DoEvent();
}

DoEvent is the function which will handle the request and connection: DoEvent是将处理请求和连接的函数:

void* DoEvent()
{
    unsigned char  buffer[1024];
    int rc;
    int    close_conn= FALSE;
    int new_sd;

    new_sd = accept(Socket, NULL, NULL);
    if (new_sd < 0)
    {
        if (errno != EWOULDBLOCK)
        {
            perror("  accept() failed");
            close_conn = TRUE;
        }
    }


    do
    {
        rc = recv(new_sd, buffer, sizeof(buffer), 0);
        if (rc < 0)
        {
            if (errno != EWOULDBLOCK)
            {
                perror("  recv() failed");
                close_conn = TRUE;
            }
            break;
        }

        if (rc == 0)
        {
            printf("  Connection closed\n");
            close_conn = TRUE;
            break;
        }

[DO SOMETHING WITH THE BUFFER]

        rc = send(new_sd, buffer, len, 0);
        if (rc <= 0)
        {
            perror("  send() failed");
            close_conn = TRUE;
            break;
        }
    }while (close_conn==FALSE);
     close(new_sd);
}

My Question is why am I receiving an error : recv() failed: Bad file descriptor???? 我的问题是为什么我会收到错误:recv()失败:错误的文件描述符? When I am adding a sleep(1) in between pthread_create and pthread_detach, everything is working! 当我在pthread_create和pthread_detach之间添加sleep(1)时,一切正常! Can somebody explain this circumstances to me? 有人可以向我解释这种情况吗? or maybe help me solving my problem? 还是可以帮助我解决问题?

Thanks! 谢谢!

Well you do try to receive even if accept fails. 那么你试着接受,即使accept失败。 This leads me to believe that you haven't properly set up the passive Socket file descriptor properly before trying to accept from it. 这使我相信您在尝试接受被动Socket文件描述符之前没有正确设置它。

If you set up the Socket descriptor in the main thread, then it could be that the new thread starts and runs before you do that. 如果在主线程中设置Socket描述符,则可能是新线程启动并运行之后再执行。

Make sure new_sd is in persistent memory. 确保new_sd在持久内存中。 eg if it is allocated by a function that launches the thread and exits, then new_sd will be pointing to junk. 例如,如果它是由启动线程并退出的函数分配的,则new_sd将指向垃圾。 (at least that was the case for me) (至少我是这种情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM