C socket programming: does recv () call change the socket file descriptor?

Hi everyone, I have this strange problem with recv (). I program the client / server, where the client sends () a message (more precisely, the structure) and the server recv (). I also work with multiple sockets and select ().

while(1)
{
    readset = info->read_set;
    info->copy_set = info->read_set;

    timeout.tv_sec = 1; 
    timeout.tv_usec = 0; // 0.5 seconds

    ready = select(info->max_fd+1, &readset, NULL, NULL, &timeout);

    if (ready == -1)
    {
        printf("S: ERROR: select(): %s\nEXITING...", strerror(errno));
        exit(1);
    }
    else if (ready == 0)
    {
        continue;
    }
    else
    {
        printf("S: oh finally you have contacted me!\n");
        for(i = 0; i < (info->max_fd+1); i++)
        {

            if(FD_ISSET(i, &readset)) //this is where problem begins
            {
                printf("S: %i is set\n", i);
                printf("S: we talking about socket %i son\n", i);  // i = 4
                num_bytes = recv(i, &msg, MAX_MSG_BYTE, 0);
                printf("S: number of bytes recieved in socket %i is %i\n", i, num_bytes); // prints out i = 0 what??

                if (num_bytes == 0)
                {
                    printf("S: socket has been closed\n");
                    break;
                }
                else if (num_bytes == -1)
                {
                    printf("S: ERROR recv: %d %s \n", i, strerror(errno));
                    continue;
                }
                else                    
                {
                    handle_request(arg, &msg);
                    printf("S: msg says %s\n", msg->_payload);
                }
            } // if (FD_ISSET(i, &readset)
            else
                printf("S:  %i is not set\n", i);
        } // for (i = 0; i < maxfd+1; i++) to check sockets for msg
    } // if (ready == -1)   

    info->read_set = info->copy_set;
    printf("S: copied\n");

} 

the problem is that read_set0 ~ 3 are not set and 4 are. This is normal. But when I call recv(), iit becomes annoying, it becomes 0. Why? It doesn't matter to me why it recv()will take the socket file descriptor number and change it to another number. This is normal? Did I miss something?

S:  0 is not set
S:  1 is not set
S:  2 is not set
S:  3 is not set
S: 4 is set
S: we talking about socket 4 son
S: i is strangely or unstrangely 0
S: number of bytes recieved in socket 0 is 40

What is he typing.

+3
source share
2

recv , .

, msg i,

printf("S: msg says %s\n", msg->_payload);

-> msg, , , , :

struct somestruct* msg = malloc(sizeof(struct somestruct));
int i;

:

num_bytes = recv(i, &msg, MAX_MSG_BYTE, 0);

, msg , &msg .

, msg, , msg. , 4 , , . i msg, , , , , .

msg , , :

num_bytes = recv(i, msg, MAX_MSG_BYTE, 0);

,

handle_request(arg, &msg)

handle_request .

+2

, sizeof(msg) < MAX_MSG_BYTE recv msg, i.

+1

Source: https://habr.com/ru/post/1741863/


All Articles