Doubt with sockets (WinSocks)

1

Good,

I'm learning how to work with sockets (I've never worked) and decided to start with winsocks. At the moment I'm using winsock2.h and in the tutorial I'm following I came to a part that I can not understand how it works.

HERE IS THE TUTORIAL

The part that I have doubts is how the handle of the various clients is done.

 //add master socket to fd set
    FD_SET(master, &readfds);
       //add child sockets to fd set
    for (  i = 0 ; i < max_clients ; i++) 
    {
        s = client_socket[i];
        if(s > 0)
        {
            FD_SET( s , &readfds);
        }
    }

    if (FD_ISSET(master , &readfds)) 
 {....}

Why is this if statement not always true and how does it work? The master socket is always set. At first I thought it was because it was done set to client sockets, but none of them is greater than 0.

    
asked by anonymous 24.07.2015 / 01:43

1 answer

1

In the tutorial, before the if in question there is a call to the select function. The select function will change the bits of readfds according to the presence or absence of activity in its socket . It is for this reason that, even with the code setting the bit corresponding to the socket "master", after the call to select this bit can be reset, and the if may not be true.

    
24.07.2015 / 16:35