C ++:无法连接到服务器套接字(linux)

时间:2016-10-20 03:22:01

标签: c++ c linux sockets sigpipe

逗人,

我有一个执行子进程的主程序,并尝试通过套接字连接到这个子进程。此连接正常。但是,当检测到错误时,主程序会尝试正确关闭套接字连接,然后它会终止子进程,重新启动新进程,并尝试连接到新的子进程。

重新连接可以工作数百次。但是,如果我重复这几百次,经过一定数量的循环,我的主程序就无法连接到子进程。

我不知道它有一个链接,但在尝试正确关闭套接字时,我的主程序有时会收到一个SIGPIPE。我忽略它并继续处理。但是,我注意到当程序不再接受连接时,它总是收到10个SIGPIPES。我试了几次,总是在10点结束。

有什么想法吗?

此致 布莱斯

已编辑:

以下是关闭连接的代码: `

int BAE_nb_sigpipes_received = 0;
void enmx_close( ENMX_HANDLE conn )
{
    sConnectionInfo         **connInfo, *temp;
    SOCKET_CMD_HEAD         cmd_head;
    time_t           secs;
    pth_event_t             ev_wakeup;

    temp = NULL;
    for( connInfo = &enmx_connections; *connInfo != NULL; connInfo = &(*connInfo)->next ) {
        if( (*connInfo)->socket == conn ) {
            temp = *connInfo;
            *connInfo = (*connInfo)->next;
            break;
        }
    }

    if( temp != NULL ) {
        cmd_head.cmd = SOCKET_CMD_EXIT;
        cmd_head.address = 0xffff;
        secs = time( NULL ) + TIMEOUT;
        switch( temp->mode ) {
            case ENMX_MODE_STANDARD:
               while( write( conn, &cmd_head, sizeof( cmd_head )) == -1 )
                {
                    if( errno == EAGAIN ) {
                        if( secs <= time( NULL )) {
                            break;
                        }
                        usleep( 10 * 1000 );
                        continue;
                    }
                    /* BAE : if we encounter a sigpipe, the client stupidly continues to try to write to scoket, which never ends...*/
                    else if (errno == EPIPE)
                    {
                       BAE_nb_sigpipes_received++;
                       break;
                    }
                }
                break;
            case ENMX_MODE_PTH:
                ev_wakeup = pth_event( PTH_EVENT_TIME, pth_time( secs, 0 ));
                pth_write_ev( conn, &cmd_head, sizeof( cmd_head ), ev_wakeup );
                // either the request has been sent or timeout reached
                // in any case, we've finished
                pth_event_free( ev_wakeup, PTH_FREE_ALL );
                break;
        }
        if( temp->hostname ) free( temp->hostname );
        if( temp->name ) free( temp->name );
        free( temp );
    }

    close( conn );
}

`

以下是打开它的代码: `

    if( connect( sock_con, (struct sockaddr *)&server, sizeof( struct sockaddr_in )) != 0 ) {
       printf("ENMX_E_SERVER_NOTRUNNING (%s)\n", myname);
       return( ENMX_E_SERVER_NOTRUNNING );
    }

`

杀死子进程的代码如下: `

wxProcess::Kill(eibnetmux_process_pid, wxSIGKILL);

`

0 个答案:

没有答案