3

我制作了一个类似于套接字服务器的软件(在基础设施中的三个节点之间发送数据包)。为了简单起见,我创建了三个线程,一个用于每个 TCP 套接字(我知道这不是最佳的,但就我的目的而言,这是最简单的选择,也是最快的),一切都很好。在服务器启动时,三个套接字执行通常的初始化(getaddr、listen、bind 等)并监听三个不同的端口;当我启动客户端时,客户端正确连接并发送/接收数据。

问题是:每当一个客户端断开连接时,甚至其他套接字也会被强制断开并重新启动,这有点问题。我敢打赌可疑代码就在这里(这是从线程启动的例程):

void ManageSocket1( void )
{
    while( true )
    {
        Socket* socket = new Socket( 27016, "SocketName" );
        if( socket->CreateSocket() )
        {
            socket->StartCommunication();
        }
        socket->CloseSocket();
        delete socket;
    }
}

void ManageSocket2( void )
{
    // clone of ManageSocket1
}

CreateSocket() 负责创建套接字,而 StartCommunication() 接收/发送数据;对于这两种方法,无需发布代码,因为它是“通用”的 tcp 套接字打开代码。ManageSocketX() 由 main() 中的线程启动,并且由于其性质而永远不会返回到 main()。

那是 CreateSocket():

int Socket::CreateSocket( void )
{
res = WSAStartup( MAKEWORD( 2,2 ), &wsaData );

if( res != 0 ) 
{
    logBook->PrintMsg( socketName, "WSAStartup failed" );
    return ERROR;
}
else
{
    logBook->PrintMsg( socketName, "WSAStartup done" );
}

res = getaddrinfo( NULL, socketPort, &hints, &result );

if ( res != 0 ) 
{
    logBook->PrintMsg( socketName, "GetAddrInfo failed" );
    WSACleanup();
    return ERROR;
}
else
{
    logBook->PrintMsg( socketName, "GetAddrInfo succesful");
}

ListenSocket = socket( result->ai_family, result->ai_socktype, result->ai_protocol );

if( ListenSocket == INVALID_SOCKET ) 
{
    logBook->PrintMsg( socketName, "GetAddrInfo failed with error: ", WSAGetLastError() );
    freeaddrinfo( result );
    WSACleanup();
    return ERROR;
}
else
{
    logBook->PrintMsg( socketName, "Socket created succesfully");
}

res = bind( ListenSocket, result->ai_addr, ( int )result->ai_addrlen );
if( res == SOCKET_ERROR ) 
{
    logBook->PrintMsg( socketName, "Bind failed with error: ", WSAGetLastError() );
    freeaddrinfo( result );
    closesocket( ListenSocket );
    WSACleanup();
    return ERROR;
}
else
{
    logBook->PrintMsg( socketName, "Socket successfully bound");
}

freeaddrinfo( result );

if( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR ) 
{
    logBook->PrintMsg( socketName, "Bind failed with error: ", WSAGetLastError() );     
    closesocket( ListenSocket );
    WSACleanup();
    return ERROR;
}
else
{
    logBook->PrintMsg( socketName, "Listening on port", atoi( socketPort ) );
}

ClientSocket = accept( ListenSocket, NULL, NULL );
if( ClientSocket == INVALID_SOCKET ) 
{
    logBook->PrintMsg( socketName, "Accept failed:", WSAGetLastError() );
    closesocket( ListenSocket );
    WSACleanup();
    return 1;
}
else
{
    logBook->PrintMsg( socketName, "Client connection accepted" );
}

return SUCCESS;
}

那就是 StartCom()

void Socket::StartCommunication( void )
{
char recvbuf[BUFLEN];
int  recvbuflen = BUFLEN;

do
{
    res = recv( ClientSocket, recvbuf, recvbuflen, 0 );
    if( res > 0 ) 
    {
        logBook->PrintMsg( socketName, "Bytes received:", res );            
    } 
    else if( res == 0 )
    {
        logBook->PrintMsg( socketName, "Connection closing..." );
    }
    else 
    {
        logBook->PrintMsg( socketName, "Receive failed:", WSAGetLastError() );
        closesocket( ClientSocket );
        WSACleanup();
        return;
    }       
} 
while( res > 0 );
}
4

1 回答 1

3

问题是WSACleanup当任何套接字终止时您正在调用。从文档中:

在多线程环境中,WSACleanup 终止所有线程的 Windows 套接字操作。

更远:

调用 WSACleanup 时打开的套接字将被重置并自动释放,就像调用 closesocket 一样。

于 2012-12-15T14:21:09.043 回答