TCP server and client implementation under Linux tutorial

  

First look at the TCP server side of the program:
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include < ;string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define portnumber 3333 //The port number of the server and the port number of the client must be equal
int main(int argc, char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size; int nbytes;char buffer [1024]; /* The server starts to create the sockfd descriptor */if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) //AF_INET:IPV4;SOCK_STREAM:TCP{ fprintf(stderr," Socket error: %s\ \\a",strerror(errno)); exit(1); } /* Server-side fill sockaddr structure */bzero(&server_addr,sizeof(struct sockaddr_in)); //Initialize, set 0server_addr.sin_family=AF_INET; //Internetserver_addr.sin_addr.s_addr=htonl(INADDR_ANY); //(will be on this machine Long data is converted to long data on the network) and any host communication //INADDR_ANY means that it can receive data of any IP address, that is, bind to all IPserver_addr.sin_port=htons(portnumber); //(will be short on this machine) Data is converted to short data on the network) Port number /* Bundle sockfd descriptor to IP address */if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Bind error:%s\ \\a",strerror(errno)); exit(1); } /* Set the maximum number of clients allowed to connect */if(listen(sockfd,5)= =-1) { fprintf(stderr,"Listen error:%s\ \\a",strerror(errno)); exit(1); } while(1) { /* The server is blocked until the client establishes a connection* /sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr,"Accept error:% s\ \\a",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s\ ",inet_ntoa(client_addr.sin_addr)); //Convert network address to . String if((nbytes=read(new_fd,buffer,1024))==-1) //This can be written as if((nbytes=recv(new_fd,buffer,1024,0))==-1) { Fprintf(stderr,"Read Error:%s\ ",strerror(errno)); exit(1); } buffer[nbytes]='\\0';printf("Server received %s\ ",buffer /* This communication has ended */close(new_fd); /* Loop next */} /* End communication */close(sockfd); exit(0); } Review the client's program:< Br>#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types. h> #include <netinet/in.h> #include <sys/socket.h> #define portnumber 3333int main(int argc, char *argv[]) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr ; struct hostent *host; /* Use hostname to query the host name */if(argc!=2) { fprintf(stderr,"Usage:%s hostname \\a\ ",argv[0]); exit(1) ; } if((host=gethostbyname(argv[1]))==NULL) { fprintf(stderr,"Gethostname error\ "); exit(1); } /* The client program starts to build the sockfd descriptor */if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) //AF_INET: Internet; SOCK_STREAM: TCP{ fprintf(stderr,"Socket Error:%s \\a\ ",strerror(errno)); exit(1); } /* The client fills the server's data */bzero(&server_addr,sizeof(server_addr)); //Initializes, sets 0server_addr.sin_family=AF_INET ////IPV4server_addr.sin_port=htons(portnumber); //(converts the short data on this machine to the short data on the network) port number server_addr.sin_addr=*((struct in_addr *)host->h_addr); //IP address /* client initiates a connection request */if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Connect Error :%s\\a\ ",strerror(errno)); exit(1); } /* The connection was successful*/printf("Please input char:\ "); /* Send data*/fgets(buffer , 1024, stdin); write (sockfd, buffer, strlen (buffer)); //here can be written as send (sockfd, buffer, strlen (buffer), 0); /* end communication * /close (sockfd); Exit(0); } The result of the sequence is as follows: (Note: the server's socket and the client's socket seem to be different. The server on the virtual machine uses gcc tcp_server.c -o tcp_server compiler on the hi3515 client uses arm-hismall- Linux-gcc tcp_client.c -o tcp_hiclient to compile the program) Server:
Client:

Copyright © Windows knowledge All Rights Reserved