只关闭一个插座

时间:2012-12-15 11:58:16

标签: c++ sockets tcp

我制作了一个软件,其作用类似于套接字服务器(在基础架构中的三个节点之间调度数据包)。 为了简单起见,我创建了三个线程,每个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 );
}

1 个答案:

答案 0 :(得分:3)

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

  

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

此外:

  

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