0

Okay, so I'm using WinSockets2 to make a pretty simple server-client application. I have my sockets initialized and I'm trying to allow the server to be multi-user. I am almost 100% sure that up to this point, the server socket is correctly formed and I am 100% sure that the client socket is correct as I used it with another server. Though, the recv function fails with error WSAENOTSOCK. The code in question:

unsigned __stdcall client(void *data) {
    SOCKET clientSocket = (SOCKET)data;

    char inBuffer[DEFAULT_BUFLEN];

    int inBytes = SOCKET_ERROR;

    ZeroMemory(inBuffer, sizeof(inBuffer)); //make sure the buffer is 0

    inBytes = recv(clientSocket, inBuffer, sizeof(inBuffer), 0);
    printf("Client said: %s and there's this also %d\n", inBuffer, inBytes);

    if (inBytes == SOCKET_ERROR) {
        printf("eek! Something went wrong! %ld\n", WSAGetLastError());
    }
    return 0;
}

and a little bit below it...

    bool serverOn = true;
    while (clientSocket = accept(listenSocket, NULL, NULL) && serverOn) {
        //http://stackoverflow.com/questions/15185380/tcp-winsock-accept-multiple-connections-clients
        unsigned threadID;
        HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &client, (void*)clientSocket, 0, &threadID);
    }

    if (iResult = shutdown(clientSocket, SD_SEND) == SOCKET_ERROR) { //clean up and end everything
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    closesocket(clientSocket);
    WSACleanup();

    return 0;
}

If any other code is needed, please ask. Thanks!

Aearnus
  • 541
  • 6
  • 21
  • If any other condition must be met to reproduce please post. Thanks. -- When does the happens? Is the `serverOn` flag set at that time? There is at least a smell: You create threads each with a socket, but when while is left the socket (probably 0, but not INVALID_SOCKET) is `shutdown` and `close`d. I would expect a handling for the `listenSocket`. But there is to little information about the context... – harper May 15 '14 at 05:31

1 Answers1

-1

Okay, I found the answer after a lot of trial and error. In this code block:

 bool serverOn = true;
    while (clientSocket = accept(listenSocket, NULL, NULL) && serverOn) {
        //http://stackoverflow.com/questions/15185380/tcp-winsock-accept-multiple-connections-clients
        unsigned threadID;
        HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &client, (void*)clientSocket, 0, &threadID);
    }

    if (iResult = shutdown(clientSocket, SD_SEND) == SOCKET_ERROR) { //clean up and end everything
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    closesocket(clientSocket);
    WSACleanup();

    return 0;
}

the line while (clientSocket = accept(listenSocket, NULL, NULL) && serverOn) { should be split into two lines,

while (serverOn) {
        clientSocket = accept(listenSocket, NULL, NULL);

I guess it has something to do with the scope of the variable. Happy coding guys!

Aearnus
  • 541
  • 6
  • 21