Server code: # include <sys/socket. h> # include <netinet/in. h> # include <sys/types. h> # include <stdlib. h> # include <stdio. h> # include <string. h> int main () {int res, listenfd, clientfd; // return value, listening socket, customer socket char buff [BUFSIZ] = ""; int len; struct sockaddr_in serveraddr; // address type bzero (& serveraddr, sizeof (serveraddr); // clear serveraddr. sin_family = AF_INET; // use the AF_INET domain serveraddr. sin_addr.s_addr = htonl (INADDR_ANY); // address type that can be connected to the local server That is, the IP type. Here serveraddr is of any type. sin_port = htons (50002); // specify the port number len = sizeof (serveraddr); listenfd = socket (AF_INET, SOCK_STREAM, 0); // create a socket bind (listenfd, (struct sockaddr *) & serveraddr, len); // name socket listen (listenfd, 23); // set the number of connectable connections. clientfd = accept (listenfd, NULL, NULL ); // accept the link printf ("connecct a client \ n"); while (strncmp (buff, "end", 3) {res = read (clientfd, buff, BUFSIZ ); // read data from the client write (2, buff, res); // display the data to the display, preferably Write to display. If printf light is used, the data left last time may be displayed. write (clientfd, "server", 6); // write data to client} close (clientfd ); // close the client close (listenfd); // close the listener exit (0);} client code: # include <sys/socket. h> # include <sys/types. h> # include <netinet/in. h> # include <stdio. h> # include <stdlib. h> # include <string. h> int main (int argc, char * argv []) {int res, connfd, len; // return value, connection socket, address length struct sockaddr_in connaddr; // The address of the server to which the client connects. It should be consistent with the address of the server. char buff [BUFSIZ]; bz Ero (& connaddr, sizeof (connaddr); // clear the socket if (argc <2) {printf ("the option is too less \ n "); exit (0);} connaddr. sin_family = AF_INET; // use the AF_INET domain connaddr. sin_addr.s_addr = inet_addr (argv [1]); // confirm the address connaddr. sin_port = htons (50002); // The port number connfd = socket (AF_INET, SOCK_STREAM, 0); // create socket len = sizeof (connaddr ); // obtain the address length connect (connfd, (struct sockaddr *) & connaddr, len); // create the link write (connfd, "Hello! ", 7); // write data to the server while (res = read (connfd, buff, BUFSIZ) {printf (" % d \ n ", res ); // display the number of received bytes res = read (fileno (stdin), buff, BUFSIZ); // read data from the server write (connfd, buff, res );} close (connfd); // close socket exit (0);} Although each server, the client sends an ACK to the other Party to confirm receipt, but the read function generally does not read it, if one side sends a FIN or RST signal, read will be processed as the end of the read file, that is, 0 is returned;