The article code comes from the embedded Linux application development standard tutorial
The first instance implements TCP communication between the server and the client.
The second instance uses the select function to achieve multiplexing on the basis of the first instance, instead of blocking all the time.
Server code
/* Server. C */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <sys/IOCTL. h> # include <unistd. h> # include <netinet/in. h> # define port4321 # define sequence main () {struct sockaddr_in server_sockaddr, client_sockaddr; int sin_size, recvbytes; int sockfd, client_fd; char Buf [buffer_size]; /* establish a socket connection */If (sockfd = so Cket (af_inet, sock_stream, 0) =-1) {perror ("socket"); exit (1) ;}printf ("socket id = % d \ n ", sockfd);/* set relevant parameters in the sockaddr_in struct */server_sockaddr.sin_family = af_inet; outputs = htons (port); outputs = inaddr_any; bzero (& (server_sockaddr.sin_zero), 8 ); int I = 1;/* bind the local address to the socket repeatedly */setsockopt (sockfd, sol_socket, so_reuseaddr, & I, sizeof (I )); /* bind function */If (BIND (sockf D, (struct sockaddr *) & server_sockaddr, sizeof (struct sockaddr) =-1) {perror ("bind"); exit (1 );} printf ("bind success! \ N ");/* call the listen function */If (Listen (sockfd, max_que_conn_nm) =-1) {perror (" listen "); exit (1 );} printf ("listening .... \ n ");/* call the accept function and wait for the client connection */If (client_fd = accept (sockfd, (struct sockaddr *) & client_sockaddr, & sin_size )) =-1) {perror ("accept"); exit (1);}/* call the Recv function to receive client requests */memset (BUF, 0, sizeof (BUF); If (recvbytes = Recv (client_fd, Buf, buffer_size, 0) =-1) {perror ("Recv "); exit (1);} printf ("received a message: % s \ n", Buf); close (sockfd); exit (0 );}
Client code
/* Client. C */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <sys/IOCTL. h> # include <unistd. h> # include <netdb. h> # include <netinet/in. h> # define port4321 # define buffer_size 1024int main (INT argc, char * argv []) {int sockfd, sendbytes; char Buf [buffer_size]; struct hostent * Host; struct sockaddr_in serv_addr; If (argc <3) {fprintf (stderr, "Usage :. /client hostname (or IP address) text \ n "); exit (1);}/* address resolution function */If (host = gethostbyname (argv [1]) = NULL) {perror ("gethostbyname"); exit (1);} memset (BUF, 0, sizeof (BUF); sprintf (BUF, "% s ", argv [2]);/* Create socket */If (sockfd = socket (af_inet, sock_stream, 0) =-1) {perror ("socket "); exit (1);}/* set the relevant parameter */serv_addr.sin_family = af_inet; serv_addr.sin_port = htons (port); serv_addr.sin_addr = * (struct in_addr *) host-> h_addr); bzero (& (serv_addr.sin_zero), 8);/* call the connect function to actively initiate a connection to the server */If (connect (sockfd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr) =-1) {perror ("Connect"); exit (1 );} /* send the message to the server */If (sendbytes = Send (sockfd, Buf, strlen (BUF), 0) =-1) {perror ("send"); exit (1) ;}close (sockfd); exit (0 );}
Below are the optimized servers
'
/* Net_select.c */# include <sys/types. h> # include <sys/socket. h> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <sys/time. h> # include <sys/IOCTL. h> # include <unistd. h> # include <netinet/in. h> # define port4321 # define seconds # define buffer_size1024int main () {struct sockaddr_in server_sockaddr, client_sockaddr; int sin_size, count; fd_set inset, tmp_inse T; int sockfd, client_fd, FD; char Buf [buffer_size]; If (sockfd = socket (af_inet, sock_stream, 0) =-1) {perror ("socket"); exit (1);} server_sockaddr.sin_family = af_inet; region = htons (port); region = inaddr_any; bzero (& (server_sockaddr.sin_zero), 8 ); int I = 1;/* bind the local address to the socket repeatedly */setsockopt (sockfd, sol_socket, so_reuseaddr, & I, sizeof (I )); if (BIND (sockfd, (Struct sockaddr *) & server_sockaddr, sizeof (struct sockaddr) =-1) {perror ("bind"); exit (1);} If (Listen (sockfd, max_que_conn_nm) =-1) {perror ("listen"); exit (1);} printf ("listening .... \ n ");/* use the descriptor that calls the socket function as the file descriptor */fd_zero (& inset); fd_set (sockfd, & inset); While (1) {tmp_inset = inset; sin_size = sizeof (struct sockaddr_in); memset (BUF, 0, sizeof (BUF);/* call the select function */If (! (Select (max_sock_fd, & tmp_inset, null, null)> 0) {perror ("select"); close (sockfd); exit (1 );} for (FD = 0; FD <max_sock_fd; FD ++) {If (fd_isset (FD, & tmp_inset)> 0) {If (FD = sockfd) {/* the server receives client connection requests */If (client_fd = accept (sockfd, (struct sockaddr *) & client_sockaddr, & sin_size) =-1) {perror ("accept"); exit (1);} fd_set (client_fd, & inset); printf ("new connection from % d (socket) \ n ", client_fd);} else/* process messages sent from the client */{If (COUNT = Recv (FD, Buf, buffer_size, 0)> 0) {printf ("received a message from % d: % s \ n", FD, Buf);} else {close (FD); fd_clr (FD, & inset ); printf ("client % d (socket) has left \ n", FD );}}} /* end of if fd_isset */}/* end of for FD */}/* end if while */close (sockfd); exit (0 );}