Network programming in linux <1>

1 Network programming API

(1) The IP address of the network layer can uniquely identify the host in the network, and the transport layer uniquely identifies the application in the host through the protocol + port. In this way, the triplet (address, protocol, port) is used to identify the process of the network.

(2) socket--->slot (vulgar hahahaha), regarded as a file descriptor, the basic philosophy of Linux is that everything is a file, so is it also possible to read and write and close such a habitual operation? Right, almost.

(3) Let's take a look at the tcp interaction process

ok look at each function in detail

The first: int socket (int domain, int type, int protocol)

Parameter introduction:

domain: the protocol domain, which determines the socekt address type

type: socket type.

protocol: Specifies the protocol.

The socket descriptor returned by calling this function is in the space of the protocol family, but there is no specific address, so if we give it an address, we will bind

The second: int bind(int socketfd,const struct sockaddr *addr,socklen_t addrlen)

Parameter introduction

addr: Points to the protocol address that Banting gives to sockfd. Varies depending on the address protocol family when socekt was created

The third and fourth: listen connect

To monitor the client, just link

Fifth: accept

When the client sends a link request, it begins to accept it. After accepting it, you can start to communicate ----> perform io operations.

Note: the accept is successful, and the return value is automatically generated by the kernel to a new descriptor.

Sixth, sixth group

read() write()

recv() send()

readv() writev()

recvmsg() sendmsg()

recvfrom() sendto()

A successful read will put back the number of bytes actually read. The 0 marks read to the end of what I see.

2 to an example

 1 //client.cpp
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<errno.h>
 6 #include<sys/types.h>
 7 #include<sys/socket.h>
 8 #include<netinet/in.h>
 9 #include<arpa/inet.h>
10 #include<unistd.h>
11 #define MAXLINE 4096
12 
13 int main(int argc, char** argv){
14     int   sockfd, n;
15     char  recvline[4096], sendline[4096];
16     struct sockaddr_in  servaddr;
17 
18     if( argc != 2){
19         printf("usage: ./client <ipaddress>\n");
20         return 0;
21     }
22 
23     if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
24         printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
25         return 0;
26     }
27 
28     memset(&servaddr, 0, sizeof(servaddr));
29     servaddr.sin_family = AF_INET;
30     servaddr.sin_port = htons(6666);
31     if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
32         printf("inet_pton error for %s\n",argv[1]);
33         return 0;
34     }
35 
36     if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
37         printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
38         return 0;
39     }
40 
41     printf("send msg to server: \n");
42     fgets(sendline, 4096, stdin);
43     if( send(sockfd, sendline, strlen(sendline), 0) < 0){
44         printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
45         return 0;
46     }
47     close(sockfd);
48     return 0;
49 }
View Code
1  # include<stdio.h> 
2  # include<stdlib.h> 
3  # include<string.h> 
4  # include<errno.h> 
5  # include<sys/types.h> 
6  # include<sys/socket. h> 
7  # include<netinet/in.h> 
8  # include<unistd.h> 
9  
10  # define MAXLINE 4096 
11  
12 int main(int argc, char** argv){
 13      int listenfd, connfd;// here Two, one is to create a socket descriptor, one is a descriptor after establishing a connection,
 14      struct sockaddr_in servaddr;
 15      char buff[4096];
16     int  n;
17 
18     if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
19         printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
20         return 0;
21     }
22 
23     memset(&servaddr, 0, sizeof(servaddr));
24     servaddr.sin_family = AF_INET;
25     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
26     servaddr.sin_port = htons(6666);
27 
28     if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
29         printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
30         return 0;
31     }
32 
33     if( listen(listenfd, 10) == -1){
34         printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
35         return 0;
36     }
37 
38     printf("======waiting for client's request======\n");
39     while (1 ){
 40          if ( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1 ){
 41              printf( " accept socket error: %s(errno: %d) " ,strerror (errno),errno);
 42              continue ;
 43          }
 44          n = recv(connfd, buff, MAXLINE, 0);
 45          buff[n] = ' \0 ' ;// Be careful to add this thing after the output string Oh
 46          printf( " recv msg from client: %s\n " , buff);
 47          close(connfd);
 48     }
49     close(listenfd);
50     return 0;
51 }
View Code
 1 all:server client
 2 server:server.o
 3     g++ -g -o server server.o
 4 client:client.o
 5     g++ -g -o client client.o
 6 server.o:server.cpp
 7     g++ -g -c server.cpp
 8 client.o:client.cpp
 9     g++ -g -c client.cpp
10 clean:all
11     rm all
View Code

Run ./server first and then ./client 127.0.0.1 as shown below

ok warmed up, what is the endianness of the next article in the summary. come on. . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324978674&siteId=291194637