Programming Sockets with a Choice

I have two nodes interacting with a socket. Each node has a read stream and a write stream to communicate with the other. Below is the code for the read stream. Communication works fine between two nodes with this code. But I'm trying to add a select function to this thread, and it gives me problems (code for selection in the comments. I will just uncomment it to add functionality). The problem is that one node does not receive messages and only timeouts. Another node receives messages from another node, but never disconnects. This problem does not exist (both nodes send and receive messages) without a choice (keeping comments / * * /).

Can anyone indicate what the problem is? Thank you

void *Read_Thread(void *arg_passed)
{   
    int numbytes;
    unsigned char *buf;
    buf = (unsigned char *)malloc(MAXDATASIZE);

    /*
    fd_set master;
    int fdmax;
    FD_ZERO(&master);
    */

    struct RWThread_args_template *my_args = (struct RWThread_args_template *)arg_passed;

    /*
    FD_SET(my_args->new_fd, &master);
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    int s_rv = 0;
    fdmax = my_args->new_fd;
    */

    while(1)
    {
        /*
        s_rv = -1;
        if((s_rv = select(fdmax+1, &master, NULL, NULL, &tv)) == -1)
        {
            perror("select");
            exit(1);
        }
        if(s_rv == 0)
        {
            printf("Read: Timed out\n");
            continue;
        }
        else
        {
            printf("Read: Received msg\n");
        }
        */
        if( (numbytes = recv(my_args->new_fd, buf, MAXDATASIZE-1, 0)) == -1 )
        {
            perror("recv");
            exit(1);
        }
        buf[numbytes] = '\0';

        printf("Read: received '%s'\n", buf);
    }
    pthread_exit(NULL);
}
+3
1

master tv select() . select().

, select() 0, master .

+3

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


All Articles