There are some basic questions you will be asked about TCP\IP and Http during your Android/iOS development process. These are questions that you may not pay much attention to during your Android/iOS development.
Network access layer
Network interconnection layer
The transport layer
The application layer
TCP/IP is usually used together, but they are two different protocols and serve different functions. IP protocol is used to find addresses, corresponding to the Internet layer, TCP protocol is used to standardize the transport rules, corresponding to the transport layer. IP is only responsible for finding the address, the specific transmission work to TCP to complete. TCP communicates three times before transmission, commonly known as “three-way handshake”. When data is disconnected after transmission, TCP communicates four times, commonly known as “four-way handshake”. To understand this process, you need to understand the two sequence numbers and three flag bits in TCP: SEq: sequence number, which indicates the sequence number of the data to be transmitted. During TCP transmission, each byte has a serial number. When sending data, the first serial number of the data is sent to the other party. The receiver checks whether the data is received according to the serial number.
Ack: the abbreviation of ASKNoledinterfaces Number, which stands for confirmation number. It is used by the receiver to feedback the data information that has been successfully received to the sender. Its value is the starting number of the next packet that it wants to receive, that is, the number represented by the ACK value. The previous data has been successfully received.
ACK: indicates the confirmation bit. ACK takes effect only when ACK=1. The ACK value is 1 during normal communication. The ACK value is 0 during the first request because there is no data to confirm receiving.
SYN: Indicates the synchronization bit, which is used to serial number the connection. First establish a connection and no history of receiving data, so the ack also can’t set up, according to the normal cannot run at this time, the role of the SYN is to solve the problem, when the receiver to receive the SYN = 1 when the message will be directly set an ack to receive seq + 1 values, to pay attention to the value is not set after check, Instead, the SYN bit is set based on the SYN bit, so that normal mechanisms can run. Note that the SYN is set to 1 on both sides of the handshake because both sides of the communication need to set an initial ACK value.
FIN: Terminates the connection after data transmission is complete.
The entire transmission process can be represented as follows:
There are three handshakes at the top and four waves at the bottom. The TCP transmission mode is dual-mode, that is, the two parties are peers and can transmit data at the same time. Therefore, both parties must be connected or closed at the same time. The three-way handshake and four-way handshake ensure the reliability of the connection. However, this mode also has disadvantages. The transmission efficiency is low, and the client needs to send data twice to establish a connection during the three-way handshake.
UDP is also a transport layer protocol. The main difference between TCP and TCP is that TCP has connections while UDP has no connections. UDP transmits the address as soon as it gets it, so TCP transmits data more reliably, while UDP transmits data faster.
The reliable TCP protocol is used for the underlying transmission of HTTP protocol by default, but it has brought great constraints to the rapid development of the Internet. Google developed a protocol based on UDP and QUIC(Quick UDP Intent Connection), which is based on the communication between TCP and UDP. But not widely used.
The TCP/IP protocol is a set of rules and does not work. The Socket is a specific implementation of the TCP/IP protocol.
# the HTTP protocol
HTTP is an application-layer protocol. After receiving data through TCP/IP, HTTP can be used.
The packet structure in HTTP is very important. HTTP packets are divided into two types: Request messaget and response Message. Both types include the first line, header, and body. The model is shown in the figure below:
The first line of a request packet contains the method (request type), URL, and HTTP version. The first line of a response packet contains the status line, including the HTTP version, status code, and short cause. The cause is optional. The header holds the properties of some key-value pairs, separated by a colon (:).
The body stores the specific content, the request message stores the POST data, and the response message stores the results to be displayed. The first line, the head and the body, and the contents of the head are separated by a carriage return newline (\r\n), and there is an extra blank line between the head and the body, that is, there are two consecutive carriage returns newline.
Methods in request packets include GET, HEAD, POST, PUT, and DELETE.
The status code in the response packet is the status in RePONse, which can be classified into five types:
- 1xx: information status code;
- 2xx: success status code. For example, 200 indicates success.
- 3xx: redirection status code. For example, 301 indicates redirection.
- 4xx: indicates the client error status code. For example, 404 indicates that the requested resource is not found.
- 5xx: indicates the server error status code. For example, 500 indicates an internal error.