Linux socket 2: connection-oriented socket communication implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.