vendredi 23 mars 2018

Use winsock.h select() and #include

I am developing a TCP-IP server for my game. It works fine but when I added #include thread (could not use angle brackets because of code formatting) to the the main header of the program it stopped working. I am not using multiple threads yet but just including thread or thread.h is causing my Server::Update function to lock up. This is it:

void Update()
{

    fd_set copy = master;
    int socketCount = select(0, &copy, nullptr, nullptr, nullptr);


    for (int i = 0; i < socketCount; i++)
    {
        SOCKET sock = copy.fd_array[i];
        if (sock == listening)
        {
            SOCKET client = accept(listening, NULL, NULL);
            FD_SET(client, &master);
            newclient(client);
        }
        else
        {
            char buf[serverbuffersize];
            ZeroMemory(buf, serverbuffersize);
            int bytesin = recv(sock, buf, serverbuffersize, 0);

            if (bytesin <= 0)
            {               
                clientdisconnect(sock);
                closesocket(sock);
                FD_CLR(sock, &master);
                break;
            }
            else
            {
                messagereceived(string(buf), sock);
            }
        }
    }
}

I am not using anything from the thread.h file in the whole but this system gets locked. And please, don't complain that there are questions like this. I tried to follow their answers but this functions gets stuck. I think that happens in select(). If possible, I would like to keep the server tasks in just a single thread but be able to use multithreading in other parts of my program. Thanks in advance.

EDIT

I am using

FD_ZERO(&master);
FD_SET(listening, &master);

before the main loop.

Aucun commentaire:

Enregistrer un commentaire