Quantcast
Channel: Winsock recv() blocking clients app - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Winsock recv() blocking clients app

$
0
0

I'm trying to create simple C++ server-client application, which allows multiple users to connect to server. The problem is that after creating new thread for each client, recv function started to crash clients app.

Here is my code for creating new thread for each client connected:

while (ClientSocket = accept(ListenSocket, 0, 0)) {
        if (ClientSocket == INVALID_SOCKET) {
            printf("\ninvalid client socket", GetLastError());
            continue;
        }
        unsigned threadID;
        HANDLE myhandleB = (HANDLE) _beginthreadex(NULL, 0, &Server::receiveMessageThread, (void *) &ClientSocket, 0,
                                                   &threadID);
    }

And here is a method which is trying to receive a messages from server:

void waitForMessage() {
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
    }

    do {
        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed with error: %d\n", WSAGetLastError());

    } while( iResult > 0 );
}

After calling:

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);

client app stops responding.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images