Network communication, as the technical support of the Internet, has been widely used in software development, whether it is Web, server, client or desktop applications, is a technology that must be mastered.

What is Network programming?

Programming technology for remote data exchange at software development level.Copy the code

The main scenarios of network programming

  • Development of Web and mobile terminal based on Http/Https;
  • Based on TCP/UDP IM, desktop application development;
  • Development of custom protocols.

To be familiar with network programming, you need to learn about network protocols first.

Network protocol

What network protocol? Network protocol is defined rules for data exchange in the network to achieve the goal of data exchange in the entire Internet if data is transmitted according to this specification. So network protocol is the basis of network communication.

One of the most famous network protocols is the TCP/IP protocol family. The TCP/IP protocol family is generally considered to be a four-tier protocol system. From top to bottom:

  • Application layer: it includes all data protocols that work with applications and use the basic network to exchange applications, such as Telnet, FTP, Http, etc.
  • Transport layer: responsible for providing end-to-end communication support for the application layer, including TCP and UDP protocols;
  • Network layer: responsible for the transmission of data packets on the network, including IP, ICMP and IGMP protocols.
  • Link layer: Handles the details of interactions with physical devices, corresponding to device drivers in the operating system;

In terms of the responsibilities of each layer of TCP/IP protocol family, the transmission of network data is from top to bottom. Taking Http as an example, the specific data transmission process is shown in the figure below:

Of course, this is just the flow of data. In fact, network data needs to be encapsulated and decomposed during transmission. The specific process is shown in the figure (picture from Baidu Photo) :

In simple terms, before data is transferred to the network from the local computer, the protocol header is added to each layer of the TCP/IP protocol family. After reaching the target host, the target host decomposes the data to obtain the required data.

Among the many network protocols, the most widely used is PROBABLY TCP (Transmission Control Protocol), which is a connection-oriented, reliable, byte stream based transport layer protocol. Its operation process is divided into three stages: establish a connection, exchange data, disconnect.

Establishing and terminating a TCP connection

Before using TCP to exchange data, establish a connection. Disconnect the connection to release resources when data does not need to be sent.

Establish a connection

The TCP connection requires a three-way handshake. The process can be described in the following scenarios:

Interviewer: Tell me about the three-way handshake when establishing a TCP connection. Xiaoming: Three handshakes? Interviewer: HMM. Xiaoming: The handshake is done. Interviewer: What?Copy the code

That’s right, from The beginning of Xiao Ming’s three-way handshake to the completion of the three-way handshake, the specific process is as follows:

  1. The client sends a SYN segment to the server to tell the client to connect to a specified port on the server.
  2. After receiving a packet from a client, the server returns an ACK segment (SYN+1) for confirming the packet from the client and a SYN segment for representing the packet from the server.
  3. After receiving the packet from the server, the client sends an ACK (SYN+1 on the server) to the server. Then, the client and the server establish a connection.

Why three handshakes instead of two?

After receiving the reply from the server, the client knows that the connection is established. However, the server does not know whether the client receives the acknowledgement packet sent by the client. Therefore, the client needs to confirm the packet sent by the server.Copy the code

Can the server receive the acknowledgement packet sent by the client?

Not necessarily, if not, then the connection will not be established, so the 3-way handshake is just a theoretical number of times to ensure that a connection is established. How about four handshakes? No, it's chicken and egg if you hold it any longer.Copy the code

disconnect

TCP disconnection requires four handshakes. Why four handshakes? This is due to the half-closed nature of TCP. The so-called semi-closed is that you can send data but cannot receive data or can only receive data but cannot send data.

The process of four-way handshake :(since the active disconnection can be sent to the client or the server, the two ends are distinguished by A and B)

  1. End A sends A FIN to end B to inform end A that the connection is about to be disconnected.
  2. End B sends an ACK to confirm the FIN sent by end A.
  3. Then end B sends A FIN to end A to inform end B that the connection is also about to be disconnected.
  4. After receiving A FIN packet from end A to end B, end A sends an ACK.

Why doesn’t end B send A FIN when confirming end A? You can send them at the same time. The purpose of the development is to consider the case that terminal B may continue to send data to terminal A after confirming with terminal A.

TCP status change process

The following figure describes the TCP state transition process (picture from Baidu Picture).

All the states in the picture correspond to the TCP establishment and connection process. The following states are briefly introduced:

LISTEN: Indicates that the server is in listening state, waiting for the connection request from the client. SYN Received: Indicates the status of the server. The server receives the connection request from the client and sends an ACK for the request (the second handshake is complete). SYN_SENT: client status after SYN or data is sent (the first handshake is complete). ESTABLISHED: After the client confirms the SYN on the server, the server is in ESTABLISHED state after receiving the ACK from the client (the three-way handshake is complete). FIN_WAIT_1: state of the actively closed end, state after sending the FIN (the first handshake on disconnection is complete); FIN_WAIT_2: the state of the end that is actively closed and the state after receiving an ACK from the other end (the second handshake is completed when the connection is disconnected); CLOSING: The state of the end that actively closes, receives the FIN from the other end and confirms it (when both the client and the server are closed at the same time); TIME_WAIT: The status of the actively closed end, and the status after receiving a FIN or (FIN and ACK) from the other end and confirming it (the last handshake is completed upon disconnection); CLOSE_WAIT: State of the passively closed end, after receiving and confirming the FIN sent by the other end (second handshake completed when disconnecting); LAST_ACK: indicates the status of the end that is closed passively, and the status after the FIN is sent (the third handshake is completed when the connection is closed). CLOSED: The connection is completely disconnected.Copy the code

After the birth of TCP/IP protocol family, each platform (Window, Unix) according to this protocol specification in the system level for the development of network programs to provide a unified interface – Socket. Through this system interface for transport layer protocol, we can quickly realize the exchange of network data through TCP/UDP protocol, but also can be used to realize application layer protocol, such as HTTP, SSL and so on.

Socket (Socket)

Socket is an interface provided by the operating system for upper-layer applications to exchange network data. It can be understood in the following scenarios:

When you call someone, you should first confirm who to call, and then confirm which number to call. By these two conditions, you can accurately contact the other party. An IP represents a host, but each host has many port numbers. Therefore, in order to accurately exchange data with an application, in addition to the IP address, you also need a port number. With these two conditions, data exchange can be realized through Socket. Thus, a Socket is equivalent to a mobile phone, and a channel can be established between two mobile phones.

Socket programming steps

The client

  1. Create scoket;
  2. Connect to the server;
  3. Send and receive data;
  4. Closing socket connections

Code implementation (Linux C programming) :

#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { int sockfd = 0, n = 0; char recvBuff[1024]; struct sockaddr_in serv_addr; if(argc ! = 2) { printf("\n Usage: %s <ip of server> \n",argv[0]); return 1; } memset(recvBuff, '0',sizeof(recvBuff)); // Create socket if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {printf("\n Error: Could not create socket \n"); return 1; } // set IP and port memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(5000); if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) { printf("\n inet_pton error occured\n"); return 1; If (connect(sockfd, (struct sockaddr *)&serv_addr, struct sockaddr *) sizeof(serv_addr)) < 0) { printf("\n Error : Connect Failed \n"); return 1; } while ((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) {recvBuff[n] = 0; if(fputs(recvBuff, stdout) == EOF) { printf("\n Error : Fputs error\n"); } } if(n < 0) { printf("\n Read error \n"); } close(scokfd); return 0; }Copy the code

The service side

  1. Create a socket;
  2. Bind IP addresses and ports.
  3. Listen for client connections;
  4. Receiving client connections;
  5. Send and receive data
  6. Close the socket connection.

Code implementation :(Linux C programming)

#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <time.h> int main(int argc, char *argv[]) { int listenfd = 0, connfd = 0; struct sockaddr_in serv_addr; char sendBuff[1025]; time_t ticks; Listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); // Bind IP address and port serv_addr. Sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); Status LISTEN (listenfd, 10); While (1) {connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); ticks = time(NULL); snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks)); // Send data to the client write(connfd, sendBuff, strlen(sendBuff)); // Close socket close(connfd); sleep(1); }}Copy the code

Code from https://www.thegeekstuff.com/2011/12/c-socket-programming/

Through the above code, we have a simple understanding of socket, but also understand the basic process of data exchange. The HTTP protocol based on TCP will be introduced in detail later.

The resources

TCP/IP protocol family

Transmission control protocol

TCP/IP Volume 1