echo client and server

Achieve the basic functions: 

    Client: sending a line of text to the server, the server shows the number of bytes received, and returns the received contents to the client.

file echo.c:   

 

file echoclient.c:   

1  / * * main client * * / 
2  int main ( int argc, char ** argv) // runs, the command line parameters passed in the main program. argc-- total number of command line arguments, comprising executable program name; argv [i] - i-th parameter; argv [0] - the executable program name. 
. 3  {
 . 4      int clientfd; // clientfd-- socket descriptor; 
. 5      char * Host, Port *, buf [MAXLINE]; // host-- IP address of the host server; port- server port number; buf-- buffer ; 
. 6      rio_t Rio; // buffer forms 
. 7  
. 8      IF (argc =! . 3 )
 . 9      {
 10          fprintf (stderr, " Usage:% S \ n-" , The argv [ 0 ]); // the error input to stderr orientation device, such as a display, a file, a printer 
. 11          Exit ( 0 );
 12 is      }
 13 is      Host = the argv [ . 1 ];
 14      Port = the argv [ 2 ]; 
 15  
16      clientfd = Open_clientfd (host, Port); // establish a connection to the host server, the client returns to an open socket descriptor, may communicate with the server immediately 
. 17      Rio_readinitb (& Rio, clientfd); // read in the clientfd, Robust I / O package, the buffered data read line, encounters '\ n' signify the end of a line, returns the number of characters actually read 
18 is  
. 19      the while (. fgets (buf, MAXLINE, stdin)! NULL =) // . fgets read lines of text from standard input to buf 
20 is     {
 21          // send a text line to the server 
22 is          Rio_writen (clientfd, buf, strlen (buf));    
 23 is          // row read from the server sent back 
24          Rio_readlineb (& Rio, buf, MAXLINE); // one line read from the IO port buf data
 25          // output to standard output 
26 is          fputs (buf, stdout); // string buf is written to the specified stream stdout, but not including the null character. 
27      }
 28      the Close (clientfd); // Close descriptor 
29      Exit ( 0 ); // normal exit 
30 }
View Code

file echoserver.c:   

1  / * * main server * * / 
2  int main ( int argc, char ** the argv)
 . 3  {
 . 4      int listenfd, connfd, Port; // listenfd-- listener descriptor; connfd-- connected descriptor; port-- listening port number; 
. 5      the socklen_t clientlen;
 . 6      struct sockaddr_storage clientaddr; // socket address structure sockaddr_storage sufficiently large 
. 7      char client_hostname [MAXLINE], client_port [MAXLINE];
 . 8  
. 9      IF (= argc! 2 )
 10      {
 11          fprintf (stderr, "Usage:% S \ n- " , the argv [ 0 ]);
 12 is          Exit ( 0 );
 13 is      }
 14  
15      listenfd = Open_listenfd (the argv [ . 1 ]); // open the port listens descriptor listening server 
16      the while ( . 1 )
 . 17      {
 18 is          clientlen = the sizeof ( struct sockaddr_storage);
 . 19          // wait for a request from a client 
20 is          connfd = the Accept (listenfd, (SA *) & clientaddr, & clientlen); // before returning to fill the other end of the customer clientaddr midrange socket address
 21  
22          //The socket address structure turn clientaddr change the host and service name string buffer client_hostname village early and client_port 
23          getnameinfo ((SA *) & clientaddr, clientlen, client_hostname, MAXLINE,
 24-              client_port, MAXLINE, 0 );
 25          printf ( " Server Connected to (% S,% S) \ n- " , client_hostname, client_port);
 26 is  
27          // call to the client service echo 
28          echo (connfd);
 29          the Close (connfd);
 30      }
 31 is      Exit ( 0 ) ;
 32 }
View Code

 

supplement:

"csapp.h" file:  https://blog.csdn.net/zhongshijunacm/article/details/53506892

linux RIO packaging: Https://Blog.Csdn.Net/u013613341/article/details/51019075

 

Reference: "In-depth understanding of computer systems."

Guess you like

Origin www.cnblogs.com/chengmm/p/11610448.html