简体   繁体   中英

Winsock Server in C++ Refusing Connections After Three Clients

I'm writing a Winsock server in C++ that collects data from a analog-to-digital converter and runs a server so that clients can log in and request the data. I currently have it running at least three threads (main thread, analog input thread, and a server thread), plus the server spins off a new thread for each connected client. I'm testing the code now and it accepts connections from a client three times, but refuses connection on the fourth attempt. This is the while loop I'm using to receive connections and spin off threads for each client. When the server is refusing connections, it's still in this loop. I know from some debugging printf statements.

while (serverActive) {
        //Accept client sockets as long as the server remains active
        ClientSocket = accept(ListenSocket, NULL, NULL);
        if (ListenSocket == INVALID_SOCKET) {
            printf("Accept failed");
            closesocket(ListenSocket);
            WSACleanup();
            return 1;
        }
        std::thread clientThr(clientHandlerThread, ClientSocket);
        clientThr.detach();
    }

This is the code for the client handler thread. All the if-elseif-else statements in the middle are just the protocol I'm setting up to communicate with clients. The tester client code I'm using closes the socket from the client side. I read some other postings where it said this is sometimes the problem, but it doesn't seem to be the problem in this case. Any thoughts?

int clientHandlerThread(SOCKET client) {
    int iResult, iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    char  * p_m6 = reinterpret_cast< char  *>(&data_ai_SC1Mod6[0]);
    char  * p_m7 = reinterpret_cast< char  *>(&data_ai_SC1Mod7[0]);
    char  * p_m8 = reinterpret_cast< char  *>(&data_ai_SC1Mod8[0]);

    //Receive data until the client closes the connection
    do {
        iResult = recv(client, recvbuf, recvbuflen, 0);
        if (iResult > 0) {
            recvbuf[iResult] = (char)0;
            printf("Received Command: ");
            printf(recvbuf);
            printf("\n");

            if (recvbuf[4] == 'V') {
                if (recvbuf[6] == '6') {
                    iSendResult = send(client, "SCXI>", 5, 0);
                    iSendResult = send(client, p_m6, 64, 0);
                    iSendResult = send(client, "<SCXI", 5, 0);
                }
                else if (recvbuf[6] == '7') {
                    iSendResult = send(client, "SCXI>", 5, 0);
                    iSendResult = send(client, p_m7, 64, 0);
                    iSendResult = send(client, "<SCXI", 5, 0);
                }
                else if (recvbuf[6] == '8') {
                    iSendResult = send(client, "SCXI>", 5, 0);
                    iSendResult = send(client, p_m8, 64, 0);
                    iSendResult = send(client, "<SCXI", 5, 0);
                }
                else {
                    iSendResult = send(client, "SCXI>Unrecognized Module<SCXI", 29, 0);
                }
            }
            else {
                iSendResult = send(client, "SCXI>Unrecognized Command<SCXI", 30, 0);
            }
            if (iSendResult == SOCKET_ERROR) {
                printf("Send failed");
                closesocket(client);
                WSACleanup();
                return 1;
            }
        }
        else {
            closesocket(client);
            WSACleanup();
            return 1;
        }
    } while (iResult > 0);

    closesocket(client);
    WSACleanup();
    return 0;
}

It starts refusing connections after the first connect thread exits, because you're incorrectly calling WSACleanup() in the thread. Remove it. You should also remove it from the accept loop. You should only call it when you are ready to exit the entire process.

NB you're testing the wrong socket in the accept loop.

NB 2 Your code makes the untenable assumption that every recv() receives one entire single message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM