Socket is the socket interface, which is actually the communication node on the network. A user or application can connect to any communication endpoint on the network and transmit data as long as it is connected to a socket. In Linux, programmers can focus on application development without worrying about communication protocols. Based on the data transmission method, the socket can be divided into connection-oriented data circulation and connectionless data transmission. The communication process is as follows:
Function example:
A, server-side program echo_serv.c
# Include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdio. h> # include <errno. h> # define ehco_port 8080 # define max_client_num10void * memsets (void * s, int C, int count) {char * xs = s; while (count --) * Xs ++ = C; return s;} # definebzero (B, Len) memsets (B), '\ 0', (LEN) int main () {int sock_fd; struct sockaddr_in serv_addr; int clientfd; struc T sockaddr_in clientaddr; charbuff [101]; socklen_tlen; int closing = 0; int N;/* Create socket */sock_fd = socket (af_inet, sock_stream, 0 ); if (sock_fd =-1) {printf ("create socket error! \ R \ n "); Return 0;} else {printf (" succress to create socket % d \ r \ n ", sock_fd );} /* setting server ADDR */bzero (& serv_addr, sizeof (serv_addr); serv_addr.sin_family = af_inet; region = htons (ehco_port); region = htons (inaddr_any ); // inaddr_any indicates that bzero (& (serv_addr.sin_zero), 8) can be communicated with any host;/* bind ADDR with soket */If (BIND (sock_fd, (struct sockaddr *) & serv_addr, sizeof (serv_addr ))! = 0) {printf ("Bind Address fail! % D \ r \ n ", errno); close (sock_fd); Return 0;} else {printf (" success to bind address \ r \ n ");} /* setting listener */If (Listen (sock_fd, max_client_num )! = 0) {printf ("Listen socket error \ r \ n"); close (sock_fd); Return 0 ;} else {printf ("success to bind listen \ r \ n");}/* create another socket */Len = sizeof (clientaddr); clientfd = accept (sock_fd, (struct sockaddr *) & clientaddr, & Len); // obtain the socketif (clientfd <= 0) {printf ("Accept error! \ R \ n "); close (sock_fd); Return 0;}/* get data from client user */while (n = Recv (clientfd, buff, 100,0)> 0) {buff [N] = '\ 0'; printf ("Number of receive bytes = % d Data = % s \ n", N, buff ); fflush (stdout); send (clientfd, buff, N, 0); If (strncmp (buff, "quit", 4) = 0) break;} Close (clientfd ); close (sock_fd); Return 0 ;}
B. Client Program echo_client.c
# Include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdio. h> # include <errno. h> # define ehco_port 8080 # definemax_command 5 void * memsets (void * s, int C, int count) {char * xs = s; while (count --) * Xs ++ = C; return s;} # definebzero (B, Len) memsets (B), '\ 0', (LEN) int main () {int sock_fd; struct sockaddr_inserv_addr; char * buff [max_command] = {"ABC", "def", "test", "hello", "quit"}; char tmp_buf [100]; socklen_t Len; int N, I; /* Create socket */sock_fd = socket (af_inet, sock_stream, 0); If (sock_fd =-1) {printf ("create socket error! \ R \ n "); Return 0;} else {printf (" succress to create socket % d \ r \ n ", sock_fd );} /* setting server ADDR */bzero (& serv_addr, sizeof (serv_addr); serv_addr.sin_family = af_inet; region = htons (ehco_port); region = htons (inaddr_any ); // inaddr_any indicates that bzero (& (serv_addr.sin_zero), 8) can be communicated with any host;/* connect server */If (connect (sock_fd, (struct sockaddr *) & serv_addr, sizeof (serv_addr) =-1) {printf ("Connect error \ r \ n"); close (sock_fd); Return 0 ;} printf ("Success connect to server \ r \ n");/* send and receive data */for (I = 0; I <max_command; I ++) {send (sock_fd, buff [I], 100,0); n = Recv (sock_fd, tmp_buf, 100,0); tmp_buf [N] = '\ 0'; printf ("Data send: % s receive: % s \ n ", buff [I], tmp_buf); If (0 = strncmp (tmp_buf," quit ", 4) break ;} close (sock_fd); Return 0 ;}
Use GCC to compile and execute these two programs on different terminals to obtain communication results.
Server result client result