select monitors on stdout and sockets

The content of selectServerInTCPIPbook.c is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>

#defineBUF_SIZE 100
void error_handling(char *buf);

int main(int argc, char *argv[])
{<!-- -->
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
struct timeval timeout;
fd_set reads, cpy_reads;

socklen_t adr_sz;
int fd_max, str_len, fd_num, i;
char buf[BUF_SIZE];
if(argc!=2) {<!-- -->
printf("Usage : %s <port>\\
", argv[0]);
exit(1);
}

serv_sock=socket(PF_INET, SOCK_STREAM, 0);
memset( & amp;serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_adr.sin_port=htons(atoi(argv[1]));
\t
if(bind(serv_sock, (struct sockaddr*) & amp;serv_adr, sizeof(serv_adr))==-1)
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");

FD_ZERO( & amp;reads);
FD_SET(serv_sock, &reads);
FD_SET(0, &reads);
fd_max=serv_sock;
char send_line[BUF_SIZE];

while(1)
{<!-- -->
cpy_reads=reads;
timeout.tv_sec=5;
timeout.tv_usec=5000;

if((fd_num=select(fd_max + 1, & amp;cpy_reads, 0, 0, & amp;timeout))==-1)
break;
\t\t
if(fd_num==0)
continue;



if (FD_ISSET(STDIN_FILENO, & amp;cpy_reads)) {<!-- -->
            FD_CLR(0, &cpy_reads);
            if (fgets(send_line, BUF_SIZE, stdin) != NULL) {<!-- -->
                int i = strlen(send_line);
                if (send_line[i - 1] == '\\
') {<!-- -->
                    send_line[i - 1] = 0;
                }
                printf("input: %s\\
", send_line);
            }
        }

for(i=1; i<fd_max + 1; i + + )
{<!-- -->
if(FD_ISSET(i, & amp;cpy_reads))
{<!-- -->
if(i==serv_sock)
{<!-- -->
adr_sz=sizeof(clnt_adr);
clnt_sock = accept(serv_sock, (struct sockaddr*) & amp;clnt_adr, & amp;adr_sz);
FD_SET(clnt_sock, &reads);
if(fd_max<clnt_sock)
fd_max=clnt_sock;
printf("connected client: %d \\
", clnt_sock);
}
else
{<!-- -->
str_len=read(i, buf, BUF_SIZE);
if(str_len==0)
{<!-- -->
FD_CLR(i, &reads);
close(i);
printf("closed client: %d \\
", i);
}
else
{<!-- -->
write(1, "message from clien:", 19);
write(1, buf, str_len);
write(1,"\\
",1);
write(i, "message from server:", 20);
write(i, buf, str_len);
}
}
}
}
}
close(serv_sock);
return 0;
}

void error_handling(char *buf)
{<!-- -->
fputs(buf, stderr);
fputc('\\
', stderr);
exit(1);
}

gcc selectServerInTCPIPbook.c -o selectServerInTCPIPbook to compile, and then listen on the ./selectServerInTCPIPbook 8080 port.

Contents inside selectClient.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#defineMAXLINE 1024
int main(int argc, char **argv) {<!-- -->
    if (argc != 3) {<!-- -->
        printf("usage: select01 <IPaddress> or <Port>");
    }



    int socket_fd = socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in server_addr;
    bzero( &server_addr,sizeof(server_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(argv[2]));
    inet_pton(AF_INET,argv[1], & amp;server_addr.sin_addr);

    socklen_t server_len = sizeof(server_addr);
    int connect_rt = connect(socket_fd,(struct sockaddr *) & amp;server_addr,server_len);
    if(connect_rt<0){<!-- -->
        printf("connect failed");
        exit(0);
    }

    char recv_line[MAXLINE], send_line[MAXLINE];
    int n;

    fd_set readmask;
    fd_set allreads;
    FD_ZERO( & amp;allreads);
    FD_SET(0, &allreads);
    FD_SET(socket_fd, & amp;allreads);
    
    for (;;) {<!-- -->
        readmask = allreads;
        int rc = select(socket_fd + 1, & amp;readmask, NULL, NULL, NULL);

        if (rc <= 0) {<!-- -->
           printf("select failed\\
");
           exit(0);
        }

        if (FD_ISSET(socket_fd, & amp;readmask)) {<!-- -->
            n = read(socket_fd, recv_line, MAXLINE);
            if (n < 0) {<!-- -->
                printf("read error\\
");
                exit(0);
            } else if (n == 0) {<!-- -->
               printf("server terminated\\
");
               exit(0);
            }
            recv_line[n] = 0;
            fputs(recv_line, stdout);
            fputs("\\
", stdout);
        }

        if (FD_ISSET(STDIN_FILENO, & amp;readmask)) {<!-- -->
            // FD_CLR(0, & amp;allreads); //Remove the content registered to the standard output of the operating system
            if (fgets(send_line, MAXLINE, stdin) != NULL) {<!-- -->
                int i = strlen(send_line);
                if (send_line[i - 1] == '\\
') {<!-- -->
                    send_line[i - 1] = 0;
                }

                printf("now sending %s\\
", send_line);
                size_t rt = write(socket_fd, send_line, strlen(send_line));
                if (rt < 0) {<!-- -->
                    printf("write failed\\
");
                    exit(0);
                }
                printf("send bytes: %zu \\
", rt);
            }
        }
    }

}

Open another terminal, gcc selectClient.c -o selectClient to compile, and then use ./selectClient 127.0.0.1 8080 to connect to your own machine 8080 code>port.
After entering good on the client, you can see the message message from server:good received by the client, and the server also received good string.

In order to verify the server’s select multiplexing, use the keyboard to enter the test string here on the server. You can see that the server outputs input: test And the client doesn’t get any output.

This article is November Day 13 study notes, the content comes from Geek Time’s “Network Programming Practice”.