TransWikia.com

Cause of duplicate and missing messages in multithreaded C++ winsock socket?

Stack Overflow Asked by Zachary Zhu on September 20, 2020

void TCPConnectionV5::startServer()
{
    /* Initialize Winsock */
    int start;
    sockaddr_in SERVER;
    SERVER.sin_family = AF_INET;
    SERVER.sin_addr.s_addr = INADDR_ANY;
    //SERVER.sin_port = htons(stoi(DEFAULT_PORT));
    SERVER.sin_port = htons(1787);
    start = WSAStartup(MAKEWORD(2, 2), &_wsaData);
    if (start != 0)
    {
        cout << "Error on WSAStartup: " << start << endl;
    }
    /* Create socket that will connect to server */
    _listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    _socketCollection.push_back(_listener);

    if (_listener == INVALID_SOCKET)
    {
        cout << "Error creating socket to connect to server: " << WSAGetLastError() << endl;
        WSACleanup();
    }

    /* Bind the socket */
    start = bind(_listener, (sockaddr*)&SERVER, sizeof(SERVER));
    if (start == SOCKET_ERROR)
    {
        cout << "Error on bind:" << WSAGetLastError() << endl;
        closesocket(_listener);
        WSACleanup();
    }
    /* Create the listener socket */
    start = listen(_listener, 16);
    if (start == SOCKET_ERROR)
    {
        cout << "Error on entering the listening state: " << start << endl;
        closesocket(_listener);
        WSACleanup();
    }
    printTime();
    cout << "Server entered listening state" << endl;

    /* Create the thread */
    sockaddr_in client;
    int clientSize = sizeof(client);
    while (true)
    {
        SOCKET messager = accept(_listener, (struct sockaddr*)&client, &clientSize);
        _socketCollection.push_back(messager);
        locker.lock();
        printTime();
        if (messager != SOCKET_ERROR)
        {
            cout << "Client Connection success!" << endl;
            cout << "Messager: " << messager << endl;
            locker.unlock();
            std::thread newThread([&] {this->exchange(messager); });
            newThread.detach();
        }
        else
        {
            locker.unlock();
        }
    }
}

DWORD TCPConnectionV5::exchange(SOCKET messager)
  {
    int bytesSent = sendMessage(messager, msg);
    if (bytesSent <= 0)
    {
        closesocket(messager);
        return -1;
    }
    int bytesReceived = receiveMessage(messager);
    if (bytesReceived <= 0)
    {
        closesocket(messager);
        return -1;
    }
}

I noticed that when the server connects with multiple clients, that there sometimes appear to be duplicate messages that send to some clients, which is accompanied by missing messages to another client application. I have mutex lock/unlock in place for sending/receiving messages, but what’s causing these duplicate/missing messages? Is there some underlying issue I have to address regarding the threads?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP