Echo_server
#include <unistd.h>#include<stdlib.h>#include<stdio.h>#include<sys/socket.h>#include<sys/types.h>#include<signal.h>#include<memory.h>#include<errno.h>#include<netinet/inch.h>#include<arpa/inet.h>intsockfd;voidSign_handler (intSigno) { if(signo==SIGINT) {printf ("Server is stop\n"); Close (SOCKFD); } if(signo==SIGCHLD) {printf ("Server Child is close\n"); Wait (0); }}voidSERVER_DO (intFD) { CharBuffer[bufsiz]; ssize_t size; while(1) {printf ("Ready to read\n"); memset (Buffer,0,sizeof(buffer)); if(Size=read (Fd,buffer,sizeof(buffer))) <0) {perror ("protocal error\n"); Break; }Else if(size==0){ Break; }Else{printf ("Number of received bytes=%ld\n", size); Buffer[size]=' /'; printf ("%s\n", buffer); if(Write (Fd,buffer,sizeof(buffer)) <0){ if(errno==epipe) { Break; } perror ("Patrol error\n"); } } }}intMainintargcChar*argv[]) { if(argc<2) {printf ("usage:%s #port \ n", argv[0]); Exit (1); } //Signal Registrationsignal (Sigint,sign_handler); Signal (Sigchld,sign_handler); /*Create socket*/SOCKFD=socket (Af_inet,sock_stream,0); /*bind socket Address*/ structsockaddr_in serveraddr; Serveraddr.sin_family=af_inet; Serveraddr.sin_port=htons (Atoi (argv[1])); Serveraddr.sin_addr.s_addr=Inaddr_any; if(Bind (SOCKFD, (structsockaddr*) &serveraddr,sizeof(SERVERADDR)) <0) {perror ("socket bind fail\n"); Exit (1); } /*Listen*/ if(Listen (SOCKFD,Ten) <0) {perror ("Socket Listen fail\n"); Exit (1); } /*Accept*/ structsockaddr_in clientaddr; socklen_t Clientaddr_len=sizeof(CLIENTADDR); while(1) { intFd=accept (SOCKFD, (structsockaddr*) &clientaddr,&Clientaddr_len); if(fd<0) {perror ("Socket Accpet fail\n"); Exit (1); } /*Fork Child Progress*/pid_t pid=Fork (); if(pid<0) {perror ("Fork Child PID error\n"); Continue; }Else if(pid==0) {close (SOCKFD);//sockfd off in a child processprintf"Client from%s\n", Inet_ntoa (CLIENTADDR.SIN_ADDR)); SERVER_DO (FD); Close (FD); Break; }Else{Close (FD); }} close (SOCKFD); return 0;}
Echo_client
#include <stdlib.h>#include<stdio.h>#include<unistd.h>#include<errno.h>#include<sys/socket.h>#include<sys/types.h>#include<signal.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<string.h>intMainintargcChar*argv[]) { if(argc<3) {printf ("usage #ip #port \ n"); Exit (1); } /*Create socket*/ intSockfd=socket (Af_inet,sock_stream,0); if(sockfd<0) {perror ("Socket Error"); Exit (1); } structsockaddr_in serveraddr; memset (&SERVERADDR,0,sizeof(SERVERADDR)); Serveraddr.sin_family=af_inet; Serveraddr.sin_port=htons (Atoi (argv[2])); Inet_pton (af_inet,argv[1],&serveraddr.sin_addr.s_addr); /**connect**/ if(Connect (SOCKFD,structsockaddr*) &serveraddr,sizeof(SERVERADDR)) <0) {perror ("Socket Connect fail\n"); Exit (1); } /*bidirectional Communication*/ CharBuff[bufsiz]; size_t size; Char*prompt=">"; while(1) {memset (buff,0,sizeof(Buff)); Write (Stdout_fileno,prompt,1); Size=read (Stdin_fileno,buff,sizeof(Buff)); if(size<0)Continue; Buff[size-1]=' /'; if(Write (Sockfd,buff,sizeof(buff)) <0) {perror ("Write msg error\n"); Continue; }Else{ if(Read (Sockfd,buff,sizeof(buff)) <0) {perror ("Read MSG error\n"); Continue; }Else{printf ("%s\n", Buff); } } } }
Linux Network programming Echo server