OSI 7 layer model
To communicate with each other, different hosts must follow the same protocol. These protocols are so complex that they are abstracted into seven layers called the OPEN System Interconnection (OSI) seven-layer model. From bottom to top:
- The physical layer
- Switches at the data link layer
- Network layer router
- The transport layer
- The session layer
- The presentation layer
- The application layer
Data is called frames at the data link layer, packages at the network layer, and segments at the transport layer.
Each of these seven layers has its own protocol, and as a software developer, we need to be familiar with the following:
- Network layer: IP, ARP, RARP, ICMP, IGMP
- Transport layer: TCP and UDP
- Application layer: HTTP, HTTPS, FTP, TFTP, SMTP, SNMP, DNS
As data passes through each layer, it is encapsulated or unpacked from the sender to the lower layer and the receiver to the higher layer.
TCP protocol
TCP works at the transport layer of OSI layer 7. This is a transmission control protocol, and is reliable, link – oriented protocol. It can:
- Divide and package data for transmission;
- Number each packet and control the sequence
- Lost in transit, retransmission, error handling
- Flow control to avoid congestion
Problem 1: since it is connection-oriented, how to ensure that both sides can have communication capability?
Host A sends data to host B. How do we know that both host A and host B are capable of sending and receiving data? At least three of them. 1. Host A sends data to host B, and host B knows that Host A has the sending capability. 2. Host B replies to host A, and Host A knows that host B has the sending capability, and host B knows that host B has the sending capability
See, in order to make sure that both host A and host B are capable of sending and receiving data, there are at least three steps. This is the famous three-way handshake. Is it very simple….
Question 1.1: How to design A more reasonable three-way handshake host A requests to communicate with host B
- The ‘ape’ who designed the TCP protocol designed A SYN (Synchronize) flag. The data is A random number named Sequence Num, or SEQ for short. Seq is a random number.
- The second host B receives the data from host A, and it needs to tell B that I have received your data. Host B knows that Host A is busy and may receive A lot of data. How can host A easily know that this is my reply data? Acknowledgment Acknowledgment An additional acknowledgment field was added: Acknowledgment NUM, or ACK. The ack value must depend on the seq value, so it’s easy to add one. Because A knows there will be A third connection, I will also send you A random SEQ number. This acknowledgment should be set to 1. This acknowledgment should be set to 1. Of course, SYN is also 1 (passing a random DEQ parameter).
- Unit 3 Host A receives host B’s reply and sees SYN=1, SEQ=1. Look again This acknowledgment is correct and notify Host B, no problem. You can communicate with me now. SYN=1, SEQ=1, ack = SEQ +1 (SYN=1, SEQ +1)
After so three, host A host B will know that they and the other party have the ability to send and receive data, the next must transmit data, or three handshakes will be in vain.
Question 2: How to transmit data Host A sends data to host B if seQ is 4000, ACK is 7000 and the data transmitted is 1514, then:
The direction of | seq | ack | size |
---|---|---|---|
A->B | 4000 | 7000 | 1514 |
B->A | 7000 | 4000 + 1514-54 = 4146 | 54 |
A->B | 4146 | 54 + 54-7000 = 7000 | 1514 |
B->A | 7000 | 4146 + 1514-54 = 4292 | 54 |
Question 3: How do I know when the data transfer is complete? Do I need to disconnect the connection? Host A sends data to host B. Step 1: After sending data from host A, set the FIN (Finish) flag bit to 1, ACK bit to 1, seQ is the last message sent, and ACK bit to seQ +1. Step 2: After receiving the data from A, host B finds that the FIN is 1 and sets the ACK value to 1, indicating the transmitted SEQ +1 and SEQ indicating the transmitted ACK value. Step 3: Host B sends the packet to A again and sets the ACK value to 1, indicating the ACK value of the previous ACK +1 and SEQ indicating the last SEQ. Step 4: User A receives A FIN reply from user B. The ACK value is 1, indicating the last SEQ +1 and the seQ value is the last ACK +1. User B closes the connection after receiving the packet. User A sends the packet and closes the connection after 2ms. This is called the four waves.
The web is just here is just my own opinion, to help you understand. More tips to follow.
Reference:
- TCP triple handshake connection and the correct understanding of SEQ and ACK numbers
- I think you understand the TCP protocol!
- TCP/IP mailman of Ming Dynasty
- Introduction to the TCP three-way handshake
- TCP four times wave introduction
- TCP three handshakes and four breakups are understood in colloquial terms