Note that there are some explanatory texts on larger screens.

plurals
  1. POClose only one socket
    text
    copied!<p>I made a piece of software which acts like a socket server (dispatches packets between three nodes in a infrastructure). To make it simple I created three threads, one for each TCP socket (I know this is not optimal but for my purposes it was the simplest option and also the fastest), and everything is working great. At server start, the three socket perform the usual initialization (getaddr, listen, bind, etc) and listen to three different ports; as I launch a client, the client properly connect and send/recv data.</p> <p>The issue is that: whenever one client disconnects, even the other sockets are forcefully disconnected and rebooted, and this is kinda a problem. I bet the suspicious code lies here (this is the routine launched from the thread):</p> <pre><code>void ManageSocket1( void ) { while( true ) { Socket* socket = new Socket( 27016, "SocketName" ); if( socket-&gt;CreateSocket() ) { socket-&gt;StartCommunication(); } socket-&gt;CloseSocket(); delete socket; } } void ManageSocket2( void ) { // clone of ManageSocket1 } </code></pre> <p>CreateSocket() is the responsible of socket creation, while StartCommunication() recv/sends data; for these two methods there is no need to post code because it's a "common" tcp socket-opening code. The ManageSocketX() are launched by threads in main() and never return to main() because of their nature.</p> <p>That's CreateSocket():</p> <pre><code>int Socket::CreateSocket( void ) { res = WSAStartup( MAKEWORD( 2,2 ), &amp;wsaData ); if( res != 0 ) { logBook-&gt;PrintMsg( socketName, "WSAStartup failed" ); return ERROR; } else { logBook-&gt;PrintMsg( socketName, "WSAStartup done" ); } res = getaddrinfo( NULL, socketPort, &amp;hints, &amp;result ); if ( res != 0 ) { logBook-&gt;PrintMsg( socketName, "GetAddrInfo failed" ); WSACleanup(); return ERROR; } else { logBook-&gt;PrintMsg( socketName, "GetAddrInfo succesful"); } ListenSocket = socket( result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol ); if( ListenSocket == INVALID_SOCKET ) { logBook-&gt;PrintMsg( socketName, "GetAddrInfo failed with error: ", WSAGetLastError() ); freeaddrinfo( result ); WSACleanup(); return ERROR; } else { logBook-&gt;PrintMsg( socketName, "Socket created succesfully"); } res = bind( ListenSocket, result-&gt;ai_addr, ( int )result-&gt;ai_addrlen ); if( res == SOCKET_ERROR ) { logBook-&gt;PrintMsg( socketName, "Bind failed with error: ", WSAGetLastError() ); freeaddrinfo( result ); closesocket( ListenSocket ); WSACleanup(); return ERROR; } else { logBook-&gt;PrintMsg( socketName, "Socket successfully bound"); } freeaddrinfo( result ); if( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR ) { logBook-&gt;PrintMsg( socketName, "Bind failed with error: ", WSAGetLastError() ); closesocket( ListenSocket ); WSACleanup(); return ERROR; } else { logBook-&gt;PrintMsg( socketName, "Listening on port", atoi( socketPort ) ); } ClientSocket = accept( ListenSocket, NULL, NULL ); if( ClientSocket == INVALID_SOCKET ) { logBook-&gt;PrintMsg( socketName, "Accept failed:", WSAGetLastError() ); closesocket( ListenSocket ); WSACleanup(); return 1; } else { logBook-&gt;PrintMsg( socketName, "Client connection accepted" ); } return SUCCESS; } </code></pre> <p>and that's StartCom()</p> <pre><code>void Socket::StartCommunication( void ) { char recvbuf[BUFLEN]; int recvbuflen = BUFLEN; do { res = recv( ClientSocket, recvbuf, recvbuflen, 0 ); if( res &gt; 0 ) { logBook-&gt;PrintMsg( socketName, "Bytes received:", res ); } else if( res == 0 ) { logBook-&gt;PrintMsg( socketName, "Connection closing..." ); } else { logBook-&gt;PrintMsg( socketName, "Receive failed:", WSAGetLastError() ); closesocket( ClientSocket ); WSACleanup(); return; } } while( res &gt; 0 ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload