asaf
asaf

Reputation: 331

Socket not open correctly when using Threads c++

Hey in trying to parctice with sockets and threading. Im trying to do a little program that accept clients from one thread and add messages from client into queue. and from another thread print the messages to console.

but for some reason when i split the proccess to 2 threads, the socket not opened correctly and the listen function return 10022 error on WSAGetLastError.

the source code:

void SlaveThread(queue<char*>* tasks);
void MasterThread(queue<char*>* tasks);

int _tmain(int argc, _TCHAR* argv[])
{
    queue<char*>* tasksQueue = new queue<char*>();

    thread SecondThread(MasterThread,tasksQueue);
    thread FirstThread(SlaveThread,tasksQueue);

    FirstThread.join();
    SecondThread.join();
    return 0;
};


void SlaveThread(queue<char*>* tasks)
{
    while (true)
    {
        if (!tasks->empty())
        {
            cout << tasks->front() << " Queue size : " << tasks->size() << endl;
            tasks->pop();
        }

        Sleep(1000);
    }
};

void MasterThread(queue<char*>* tasks)
{
    WSAData WinSockData;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &WinSockData);

    /* Create socket structure */
    SOCKADDR_IN Server;
    Server.sin_addr.s_addr = inet_addr("10.0.0.7");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(27015);

    SOCKET ListenSock = socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET Connect;
    bind(ListenSock, (SOCKADDR*)&Server, sizeof(Server));
    listen(ListenSock, 1);
    int errno1 = WSAGetLastError();
    cout << "Listening on port 27015" << endl;
    //char buffer[200];
    int size = sizeof(Server);
    while (true)
    {
        if (Connect = accept(ListenSock, (SOCKADDR*)&Server, &size)){
            int errno1 = WSAGetLastError();
            cout << "Connection established.. " << endl;
        }
        //if (recv(ListenSock, buffer, 200, 0) > 0)
        //{
        //  tasks->push(buffer);
        //}
    }

    WSACleanup();
};

any suggestion why it's break when i add threads to program? because on empty program just opening the socket with the same code it's work properly.

Upvotes: 0

Views: 275

Answers (1)

Moby Disk
Moby Disk

Reputation: 3851

1) Use INADDR_ANY 2) Use ::bind instead of bind. The name is ambiguous to an standard template library call. This is why using namespace std is bad.

void MasterThread(queue<char*>* tasks)
{
    WSAData WinSockData;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &WinSockData);

    /* Create socket structure */
    SOCKADDR_IN Server;
    Server.sin_addr.s_addr = INADDR_ANY;//inet_addr("10.0.0.7");
    Server.sin_family = AF_INET;
    Server.sin_port = htons(27015);

    SOCKET ListenSock = socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET Connect;
    ::bind(ListenSock, (SOCKADDR*)&Server, sizeof(Server));
    int errno0 = WSAGetLastError();
    listen(ListenSock, 1);
    int errno1 = WSAGetLastError();
    cout << "Listening on port 27015" << endl;
    //char buffer[200];
    int size = sizeof(Server);
    while (true)
    {
        if (Connect = accept(ListenSock, (SOCKADDR*)&Server, &size)){
            int errno1 = WSAGetLastError();
            cout << "Connection established.. " << endl;
        }
        //if (recv(ListenSock, buffer, 200, 0) > 0)
        //{
        //  tasks->push(buffer);
        //}
    }

    WSACleanup();
};

Upvotes: 2

Related Questions