Use epoll as a transit station for two client socket

problem description

I set up a socket server, when there are other client connections, and if I enter key123, at the beginning, then I assume that it is the master client, to monitor its in and out,. If the input after the connection is not key123, then there can be multiple

slaves from client, and client.

characters entered from client are forwarded by server to master client, master client and sent in groups to all slave client

if a manual method such as telnet 127.0.0.1 2323, there is no problem,

but if I write a program in C to simulate manual input, the main client fails to read quickly, and the state of the server is not as good as expected

the environmental background of the problems and what methods you have tried

I"ve tried to use nonblocking, but it doesn"t work.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

socket server



    char message[BUF_SIZE];

    int nread, nwrite;
    int times = 0;

    const char * key="key123\n";
    printf("key len=%d\n", strlen(key));

-sharpif 1
    while ((nwrite = write(sock,key,strlen(key)+1))<0)
    {
        if(nwrite == -1){
            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                //sleep(1);
                printf("nwrite: try again!\n");
                continue;
            } else {
                perror("read error");
                exit(1);
            }
        }
    }
-sharpendif
//    nwrite = write(sock,key,strlen(key)+1);
    if (nwrite == 0) {
        fprintf(stderr, "nwrite: the client closed\n");
//        exit(1);
    } else {
            printf("HW connected\n");
    }
    
    //keep communicating with server
    int count = 0;
    while(1)
    {
        sprintf(message, "%d\n", countPP);
        
        //Send count
        printf("[Send]: %s", message);
        if( (nwrite=write(sock, message, strlen(message)+1)) < 0)
        {
            perror("write failed");
            exit(1);
        } else {
            if( nwrite == 0)
            {
                printf("nwwrite: no message written to svr\n");
            } else {
                printf("writen done\n");
            }
        }
-sharpif 1
        //Receive a reply from the server
        while( (nread =read(sock, message, BUF_SIZE)) <= 0)
        {
            if(nread <0)
            {
                perror("read failed");
                exit(1);
            } else {
                printf("nread: 0\n");
                continue;
            }
        }

        
        printf("[Recv]: %s\n", message);
-sharpendif
        usleep(CLIENT_INTERVAL);
    }
    
    close(sock);
    exit(0);
}

run. / go first

then run in another window. / to

what result do you expect? What is the error message actually seen?

I expect to see. / to running all the time, and then it sends out 0,1,2,3. Will be received by socket server

once there is another client (telnet 127.0.0.1 2323, and then enter the character enter casually), then the message of the master client will be sent to the slave client, and the message of the slave client will be sent to the master client

.

actually. / to makes mistakes when read

read failed: Resource temporarily unavailable

instead of receiving the number "0" from the main client on the socket server side, it is an empty character (0)

.

read message from hw is:

Mar.31,2021

The reason for

read failed is caused by asynchronism. If EAGAIN, is returned, it needs to be specially handled

. The reason for

read message has not been found yet

Menu