recv()失败:错误的文件描述符c ++ Linux [英] recv() failed: Bad file descriptor c++ Linux

查看:333
本文介绍了recv()失败:错误的文件描述符c ++ Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我的程序包括在sam etime的10 TCP服务器。一旦注意到客户端的请求,相应的tcp服务器套接字将接受连接并在单独的线程中处理它。我知道这不是解决我的实际问题的最有效的方法,但是好吧。

I have a problem. My program includes 10 TCP Server at the sam etime. 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. I know this is not the most efficient way of solving my actual problem but okay..

在主要我有一个for循环,将调用对象这是StartThread()

In main I have a for loop that will call a function in an object called Peer which is 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.

StartThread函数将在每个对等对象中调用:

The function StartThread will be called in each peer object:

    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是处理请求和连接的函数:

DoEvent is the function which will handle the request and connection:

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);
}

我的问题是为什么我收到错误:recv文件描述符????
当我在pthread_create和pthread_detach之间添加一个sleep(1)时,一切正常工作!
有人可以向我解释这种情况吗?或者可以帮助我解决我的问题。

My Question is why am I receiving an error : recv() failed: Bad file descriptor???? When I am adding a sleep(1) in between pthread_create and pthread_detach, everything is working! Can somebody explain this circumstances to me? or maybe help me solving my problem?

谢谢!

推荐答案

您可以尝试接收,即使接受失败。这使我相信你没有正确设置被动的 Socket 文件描述符,然后尝试接受它。

Well you do try to receive even if accept fails. 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.

这篇关于recv()失败:错误的文件描述符c ++ Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆