Doubt with sockets(Winsocks)

Asked

Viewed 79 times

2

Good,

I am learning how to work with sockets (never worked) and decided to start with winsocks. I am currently using winsock2.h and in the tutorial I’m following I came to a part that I can’t understand how it works.

HERE’S THE TUTORIAL

The part I doubt is how the Handle of the various customers is made.

 //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 this if statement is not always true and how it works? The socket master is always set. At first I thought it was because it was done setto clients sockets, but none of them is higher than 0.

  • I recommend using a crossplatform library, such as boost.

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 not of activity in the respective socket. This is why, even with the code setting the bit corresponding to the socket "master", after the call to select this bit can be reset, and if may not be true.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.