Summary of Socket network programming, socket network programming
Currently, almost all C/C ++ background programs require network communication. There are two Implementation Methods: Using the underlying system socket or using the existing encapsulated network library. This article summarizes the two methods and introduces a lightweight network communication library ZeroMQ.
1. Basic Scoket Programming
There is a lot of information on the basic scoket Programming Network. The author will give a brief description of the content in this article.
Socket-based programming involves the following six steps:
- 1. socket () function
- 2. bind () function
- 3. listen () and connect () Functions
- 4. accept () function
- 5. read () and write () Functions
- 6. close () function
The following describes the code in the article.
1 // server 2 3 # include <stdio. h> 4 # include <stdlib. h> 5 # include <string. h> 6 # include <errno. h> 7 # include <sys/types. h> 8 # include <sys/socket. h> 9 # include <netinet/in. h> 10 11 # define MAXLINE 4096 12 13 int main (int argc, char ** argv) 14 {15 int listenfd, connfd; 16 struct sockaddr_in servaddr; 17 char buff [4096]; 18 int n; 19 20 if (listenfd = socket (AF_INET, SOCK_STREAM, 0) =-1) {21 printf ("create socket error: % s (errno: % d) \ n", strerror (errno), errno); 22 exit (0 ); 23} 24 25 memset (& servaddr, 0, sizeof (servaddr); 26 servaddr. sin_family = AF_INET; 27 servaddr. sin_addr.s_addr = htonl (INADDR_ANY); 28 servaddr. sin_port = htons (6666); 29 30 if (bind (listenfd, (struct sockaddr *) & servaddr, sizeof (servaddr) =-1) {31 printf ("bind socket error: % s (errno: % d) \ n", strerror (errno), errno); 32 exit (0 ); 33} 34 35 if (listen (listenfd, 10) =-1) {36 printf ("listen socket error: % s (errno: % d) \ n ", strerror (errno), errno); 37 exit (0 ); 38} 39 40 printf ("======= waiting for client's request =====\ n"); 41 while (1) {42 if (connfd = accept (listenfd, (struct sockaddr *) NULL, NULL) =-1) {43 printf ("accept socket error: % s (errno: % d) ", strerror (errno), errno); 44 continue; 45} 46 n = recv (connfd, buff, MAXLINE, 0 ); 47 buff [n] = '\ 0'; 48 printf ("recv msg from client: % s \ n", buff); 49 close (connfd ); 50} 51 52 close (listenfd); 53}
1 client 2 3 # include <stdio. h> 4 # include <stdlib. h> 5 # include <string. h> 6 # include <errno. h> 7 # include <sys/types. h> 8 # include <sys/socket. h> 9 # include <netinet/in. h> 10 11 # define MAXLINE 4096 12 13 int main (int argc, char ** argv) 14 {15 int sockfd, n; 16 char recvline [4096], sendline [4096]; 17 struct sockaddr_in servaddr; 18 19 if (argc! = 2) {20 printf ("usage :. /client <ipaddress> \ n "); 21 exit (0); 22} 23 24 if (sockfd = socket (AF_INET, SOCK_STREAM, 0) <0) {25 printf ("create socket error: % s (errno: % d) \ n", strerror (errno), errno); 26 exit (0 ); 27} 28 29 memset (& servaddr, 0, sizeof (servaddr); 30 servaddr. sin_family = AF_INET; 31 servaddr. sin_port = htons (6666); 32 if (inet_ton (AF_INET, argv [1], & servaddr. sin_addr) <= 0) {33 printf ("inet_ton error for % s \ n", argv [1]); 34 exit (0 ); 35} 36 37 if (connect (sockfd, (struct sockaddr *) & servaddr, sizeof (servaddr) <0) {38 printf ("connect error: % s (errno: % d) \ n ", strerror (errno), errno); 39 exit (0); 40} 41 42 printf (" send msg to server: \ n "); 43 fgets (sendline, 4096, stdin); 44 if (send (sockfd, sendline, strlen (sendline), 0) <0) 45 {46 printf ("send msg error: % s (errno: % d) \ n ", strerror (errno), errno); 47 exit (0); 48} 49 50 close (sockfd ); 51 exit (0); 52}