Principle and Introduction of TCP/IP Protocol in Python Web Study Notes

HTTP, FTP, SMTP, Telnet, etc. protocols, oh! The HTTP protocol is the protocol used to access web pages, and then... In fact, you know, we should understand it from the reality, understand the role and function of the network protocol, and then fundamentally Let's see how he does it. We can start with common protocols such as HTTP, FTP, DHCP, DNS, ARP, etc. to understand how it works, and then follow this attitude to learn more about other protocols. Let's get to the point without further ado.

1. What is TCP/IP?

As the name implies, TCP/IP (Transmission Control Protocol/Internet Protocol), Transmission Control Protocol/Internet Protocol , TCP / IP is the core technology of the modern Internet and is defined by the IETF. All systems, terminals, lines, users and developers must abide by the rules stipulated by the TCP/IP protocol suite! The fundamental law of the IP world is the TCP/IP protocol suite. Here I'm talking about the protocol family again, just like the happy family, not only the familiar He Jiong and Xie Na. Not only the host, but also the director, camera and other staff. TCP/IP is also like a group, a family of protocols. This also includes HTTP , Telnet, FTP, RIP, UDP, and more. Its name comes from two of the most important protocols - TCP and IP. They are at the second and third layers of the TCP/IP model, respectively.

2. OSI reference model and TCP/IP reference model

The IP world is governed by communication entities, and communication entities are layered. The benefits of layering are:

1. Each layer is independent.

2. Good flexibility.

3. The structure can be separated.

4. Easy to implement and maintain.

5, can promote standardization work.

Therefore, communication entities must be divided into layers to ensure that various network technologies can coexist harmoniously and cooperate well, and continuously stimulate technological innovation. So in 1984 , the International Organization for Standardization (ISO) established a very abstract hierarchical structure, which is the famous ISO/OSI (International Organization for Standardization Development Network Architecture), which is very helpful for us to analyze network communication .

Physical layer (layer 1): The physical layer solves the most basic transmission channel, mainly involving the mechanical, electrical/optical, functional and procedural characteristics required to establish, maintain and release physical links, such as fiber optic cables How to resist attenuation, how to improve the transmit power of wireless devices , why twisted pair should have shielding layer, etc.

Data Link Layer (Layer 2): On the basis of "bit" services at the physical layer, the data link directly provides simple, frame-based data transmission in adjacent network nodes, and is also responsible for flow control, Error control (signals may be wrong due to machine, electrical and other reasons). From the " Ethernet frame " we often hear, we can know that Ethernet belongs to this layer.

Network layer (the third layer): The network layer provides two services to the upper layer----transport layer, one is called " connection-oriented " network service, and the other is called " connectionless " network service. The network layer is responsible for four major tasks: 1. Routing selection 2. Congestion control 3. Interconnection between LANs 4. Statistics and control. The IP protocol we talk about plays an important role in this layer.

Transport Layer (Layer 4): The task of the transport layer is to provide users with reliable and transparent end-to-end data transmission, as well as error control and flow control mechanisms. Because of its existence, the design of session layer, presentation layer and application layer does not need to consider the underlying details, so it plays the role of "connecting the previous and the next". If two computers A and B need to communicate, then A and B communicate directly at the transport layer, and if there are several network nodes between A and B, such as routers X, Y, Z, then A and X, X Communication with Y, Y and Z, Z and B must be carried out at the third layer. TCP and UDP belong to the transport layer.

Session layer (layer 5): The session layer provides many value-added services, such as interactive dialogue management, allowing one-way interaction, two-way switching, and two-way simultaneous conversation; managing user logins to remote time-sharing systems; transferring files between two machines, Synchronous control, etc.

Presentation layer (sixth layer): The presentation layer deals with the representation of data exchanged between communication processes, including syntax conversion, data format conversion, encryption and decryption, compression and decompression, etc.

The seventh layer (application layer): The application layer is the so-called product standing on the shoulders of giants. The ultimate purpose of all the underlying protocols is to provide a reliable means of transmission for the application layer. The underlying protocols do not directly hide any actual needs of users. . Sending and receiving emails, sending files, streaming web pages, and interacting with real-time information that we use every day belong to the application layer and are the most intuitive services for user experience.

As the so-called "survival of the fittest, survival of the fittest", OSI has gradually withdrawn from the attention of people due to its complex system and inconvenient computer software implementation. TCP/IP has been widely used.

Comparing the seven-layer structure of ISO/OSI, readers will be surprised: how come there is no presentation layer and session layer? Indeed, these two layers are not necessary in the TCP/IP reference model. There is also a four-layer model in which the data link layer and the physical layer are represented as the network interface layer. The core part of the TCP\IP architecture is the upper three layers: the application layer, the transport layer, and the network layer.

(1) Network interface layer: TCP/IP does not strictly define this layer, it only requires an access interface that can be provided to its upper layer-----network layer in order to transmit IP packets on it. Since this level is not defined, its specific implementation method will vary with different network types. Ethernet is the most common form of data link layer in IP communication. In addition, PPP , HDLC and other Ethernet below 10Gbit/s are generally used in local area networks, while PPP and HDLC are mostly used in wide area networks.

(2) Network layer: commonly known as the IP layer, it handles the communication between machines. IP is an unreliable, connectionless protocol that receives requests from the transport layer and transmits a packet with destination address information. IP is responsible for assigning an address to each computer or terminal on the Internet, and transmitting information to the correct destination in the form of IP packets. This is the core of TCP/IP. The most critical in the IP protocol suite is the routing protocol .

(3) Transport layer: Transport Layer Control Protocol (TCP) and User Datagram Protocol (UDP) are important protocols in this layer. TCP is a connection-oriented, reliable protocol. It sends a stream of bytes from one host to other hosts on the Internet without error. On the sender side, it is responsible for dividing the byte stream sent by the upper layer into segments and passing it to the lower layer. At the receiving end, it is responsible for reassembling the received message and delivering it to the upper layer. TCP also handles end-to-end flow control ( sliding window mechanism ). UDP is an unreliable and connectionless protocol, which is mainly suitable for occasions where packet ordering and flow control are not required. (For example, in the transmission of information such as important documents and text messages, etc., it is suitable for TCP and video transmission can use UDP).

Each level also corresponds to different protocols as shown in the figure:

3. TCP packet format

A TCP message includes a header (header) and a data part, and the specific fields of the header are as follows.

(1) The source port number and destination port fields—each occupies 16 bits.

(2) Serial number field-----SEQ serial number, occupying 32 bits. Each byte in the data stream transmitted in the TCP connection is programmed with a sequence number, and the value of the sequence number field is the sequence number of the first byte of the data sent in this segment.

(3) Confirmation sequence number-----ACK sequence number, 32bit, is the sequence number of the first byte of the data that is expected to be received by the other party to write the next segment. The confirmation sequence number field is valid only if the ACK flag bit is 1, ACK=SEQ+1.

(4) Flag bit: there are 6 as follows

URG-----emergency signal indicates the urgent pointer (urgent pointer), it can tell the system that this segment has urgent data and should be transmitted as soon as possible.

ACK-----Acknowledgment signal The acknowledgment number field is valid only when ACK=1. When ACK=0, the acknowledgment number is invalid.

PSH-----Push signal When receiving the segment with the TCP push bit set to 1, it will be delivered to the receiving application process as soon as possible without buffering.

RST-----Reset signal When RST=1, it indicates that there is a serious error in the TCP connection, release the connection, and then re-establish the connection.

SYN-----The synchronization signal SYN indicates a connection request or connection reception message.

FIN-----The termination signal is used to release a connection. When FIN=1, it indicates that the data of the sender of the segment has been sent, and the transport connection is required to be released.

(5) Data offset-----occupies 4 bits, which is the position from the beginning of the data of the outgoing segment to the beginning of the TCP segment.

(6) Window field-----16bit, used to control the amount of data sent by the other party, the unit is byte. One end of the TCP connection determines its own receiving window size according to the set buffer space size, and then notifies the other party to determine the upper limit of the other party's sending window.

(7) Checksum ----- occupy 16 bits, check the header and data of the message segment, and add a 12-byte pseudo header before checking.

(8) Emergency pointer field-----16bit. The urgent pointer indicates the sequence number of the last byte of urgent data in this segment.

(9) Option field ---- no fixed length, TCP only specifies one option, the maximum segment length MSS.

(10) Reserved field-----6 bits, no.

4. TCP's three-way handshake and four-way wave

The connection and establishment of TCP adopts the client-server method. The process that actively initiates the establishment of the connection is called the client. A server that passively waits for a connection to be established is called a server.

Establishing a TCP connection requires three handshakes as shown in the figure

 

The first handshake: Client (host A) wants to send a connection request to Server (host B). In this packet, the flag bit SYN=1, and the sequence number SEQ=x is sent. In the above figure, x=200, and the Client enters the SYN_SEND state. Wait for Server confirmation.

The second handshake: After the server receives the data packet, the flag bit SYN=1 knows that the client requests to establish a connection. The server sets the flag bit SYN and ACK to 1, confirms the serial number ACK=x+1=201, and randomly interprets a sending serial number y, let y=500. And send the data packet to the Client to confirm the connection request, and the Server enters the SYN_RCVD state.

The third handshake: After the client receives the person, check whether the confirmation serial number ACK is x+1=201; whether the flag bit ACK is 1, if it is correct, set the flag bit ACK to 1, and confirm the serial number ACK=y+1= 501, and send the data packet to the server. The server checks whether the required number ACK is y+1=501, and whether the flag bit is 1. If it is correct, the connection is established successfully, and the client and server enter the ESTABLISHED state.

To understand visually, the client is Xiaoming, the server is Xiaohong, the label of Xiaoming's email to Xiaohong is SEQ, and the SYN is the intention of Xiaoming to deal with the object in the text message. After receiving it, Xiaohong also sent a letter to Xiaoming. SEQ is also the number of Xiaohong's email. ACK=SEQ+1 means that she has received Xiaoming's previous message. The position of the ACK mark is 1, indicating that Xiaohong agrees with Xiaoming. The object's request, SYN also indicates that Xiaohong has the intention to get along with the object. So Xiaoming started to send a letter saying that I was going to start chasing you. Then the two started chatting about the future... Three handshake to the future~~~~~~! So guys have to be proactive.

(There will be small discrepancies in image understanding)

 

Four waves of hands to disconnect a TCP connection

The first wave: When client A wants to disconnect the TCP connection, it sends a packet with the flag bit FIN=1, ACK=1, the sending sequence number SEQ=X, the confirmation sequence number ACK=Z, and the client enters the FIN_WAIT state.

The second wave: After client B knows that A is about to disconnect, it sends an acknowledgement packet, in which the flag bit ACK=1, the sequence number SEQ=Z is sent to confirm the sequence number ACK=x+1, and the server enters the CLOSE_WAIT state.

The third wave: Client B also disconnects the TCP connection, and sends a packet at this time, in which the flag bit FIN=1, and the sending sequence number SEQ=Z+1

, Server enters LAST_ACK state.

The fourth wave: After client A receives the disconnection request from B, the client enters the TIME_WAIT state, and then sends an acknowledgement packet, the flag bit ACK=1, the sending sequence number SEQ=x+1, the acknowledgement sequence number ACK=Z+2, Server Enter the CLOSE state.

Also give a chestnut, I won't compare Xiaohong and Xiaoming, after all, they have just entered a state of love, and then Xiaotao and Xiaocui. Men become bad when they have money. Xiaotao is a scumbag, so he first signed the divorce agreement and some documents of his own (requiring Xiaocui to handle it by himself) (ack), and then sent it with his own breakup letter (seq). Xiaocui. ACK is set to 1, FIN is set to 1. After Xiao Cui received the letter, she didn't say a word. She replied with a letter and explained that she had received Xiao Tao's letter of breaking up and agreed (seq+1), and then handed over the certificate (ack). Finally, wait for the divorce certificate to come down (ack+1) and then send the divorce certificate to Xiaotao (ACK=1, FIN=1). In the end, Xiaotao shamelessly laughed at Xiaocui (seq+1) ACK=1.

5. Common terms of TCP/IP protocol system

(1 package

A packet is a piece of data transmitted on a network, also known as a packet. Packet is a general term. In different protocols and layers, packets have different names. For example, in TCP/IP, packets at the data link layer are called frames, packets at the IP layer are called IP datagrams, and packets at the TCP layer are called IP datagrams. The packets are often called TCP packets and so on. Most packets consist of headers and information: headers often include source and destination addresses, packet length and type indicators, etc.

(2) Package

The peer layers of different devices rely on encapsulation and decapsulation to communicate with each other. Encapsulation is like getting dressed naked after a shower, and unpacking is like undressing before a bath, taking off layer after layer until you are naked. TCP/IP is also a standard method for packaging and addressing computer data. In data transmission, it can be visually understood that there are two envelopes, TCP and IP are like envelopes, the information to be transmitted is divided into several segments, each segment is stuffed into a TCP envelope, and segment information is recorded on it, and then Stuffed into a large IP envelope. Similarly, the receiving end is disassembled and verified step by step. If an error is found, the TCP mechanism will require retransmission. Therefore, TCP/IP can transmit data in the Internet almost without error.

(3) Data segment, data packet. Data frame

During the communication process, there is a protocol data unit (Protocol Data Unit) between the TCP/IP layers to exchange information with each other to ensure that the network devices can communicate. The PDU obtained by the transport layer adding the TCP header on the basis of the upper layer data is called a data segment (Segment), and the data segment is passed to the network layer in turn, and the PDU obtained by adding an IP header is called a data packet (Packet), and the data packet is passed to the network layer. The PDU obtained by adding a header to the data link layer is called a data frame (Frame). Finally, the Frame is converted into bits and propagated through the network medium at the physical layer.

 

The basic introduction of TCP/IP is here. If there are students who want to study in depth, please continue to study without fear of fatigue. Only perseverance will pay off.

 

 

refer to:

https://zhuanlan.zhihu.com/p/29478691

 

Guess you like

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