Home Operation and Maintenance Safety Detailed explanation of TCP's three-way handshake and four-way wave

Detailed explanation of TCP's three-way handshake and four-way wave

Nov 29, 2019 pm 04:43 PM
tcp

Detailed explanation of TCP's three-way handshake and four-way wave

1. TCP (Transmission Control Protocol)

TCP is connection-oriented. A reliable process-to-process communication protocol
TCP provides full-duplex service, that is, data can be transmitted in both directions at the same time

2. TCP segment (encapsulation) In IP datagram)

Detailed explanation of TCPs three-way handshake and four-way wave

1, port number

1) Source port number: the port number corresponding to the sender process , the function of the source IP and port is to mark the return address of the message.

2) Target port number: Corresponds to the process of the receiving end. After the receiving end receives the data segment, it maps the data to the application interface based on this port.

Note: The source port number and destination port number in the TCP header are the same as the source IP and destination IP in the IP datagram to uniquely determine a TCP connection.

2. Sequence number: The sending end numbers each byte to facilitate correct reassembly by the receiving end.

3. Confirmation number: used to confirm the information from the sender.

4. Control bits

1) URG: Emergency pointer valid bit.
2) ACK: Confirm sequence number bit. When this bit is 1, it is used to confirm the sender's data.
3) PSH: When the flag bit is 1, the receiving amplifier is required to deliver the data end to the application layer as soon as possible.
4) RST: Re-establish the TCP connection when it is 1
5) SYN: Synchronization sequence number bit, set this value to 1 when TCP needs to establish a connection
6) FIN: When TCP disconnects Set this position to 1

5. Window value: Used to indicate the number of locally receivable data segments. The window size is variable. This controls the rate at which the sending end sends data, thereby achieving flow control.

6. Checksum: used for error control

7. Emergency pointer: The emergency pointer is valid only when the URG flag is set to 1.

8. Options: 40 bytes of optional information located in the TCP header. The most common optional field is the longest message size.

Note: The more important of the 8 fields are the port number, sequence number, confirmation sequence number, and the three control bits of ACK, SYN, and FIN.

3. TCP three-way handshake (packet capture analysis through wireshark)

Detailed explanation of TCPs three-way handshake and four-way wave

##PC1 is For a real machine, PC2 is a virtual machine CentOS. PC1 and PC2 are bound to the same virtual network card VNet8. The IP address of PC1 is 192.168.90.10 and the IP address of PC2 is 192.168.90.40. Build the account website on CentOS in advance, PC1 accesses PC2, and then uses the packet capture tool to capture TCP reports, as shown below:

1. First handshake

Detailed explanation of TCPs three-way handshake and four-way wave

As shown above, through the packet capture tool, it is found that the source address (source) is 192.168.90.10, the source port number (source port) is 55604, the destination address (destination) is 192.168.168.90.40, and the destination port number (destination port) is 80. The initial sequence number (sequence number) and the confirmation sequence number (acknowledgment number) are both 0. In the first handshake, PC1 uses a random port number to send a connection establishment request to port 80 of PC2. The most typical representation of this process is that the SYN control bit of TCP is 1 and the other five control bits are all 0.

2. The second handshake

Detailed explanation of TCPs three-way handshake and four-way wave

As shown above, the second handshake, the source address, port number, destination address and port are the same as the first handshake Instead, the initial sequence number is 0, the acknowledgment sequence number is 1, and both ACK and SYN in the control bits are 1. The second handshake is actually completed in two parts:

1) PC2 receives the request from PC1 and replies with a confirmation message to PC1. The sign of this process is that the TCP ACK control bit is 1 and the other five control bits are all 0. , and confirm that the serial number is the initial serial number of PC1 plus 1.
2) PC2 also sends a request to establish a connection to PC1. The flags of this process are the same as the first handshake. The SYN control bit of TCP is 1 and the other five control bits are all 0.

3. The third handshake

Detailed explanation of TCPs three-way handshake and four-way wave

As shown in the figure, the source address, port number, destination address and port number are the same as the first handshake, where the initial The sequence number is the confirmation sequence number 1 of the second handshake, and the confirmation sequence number is the initial sequence number of the second handshake plus 1. When PC1 receives PC2's reply (including request and confirmation), it also needs to reply with a confirmation message to PC2. The most typical sign of this process is that the TCP ACK control bit is 1, the other five control bits are 0, and the confirmation sequence number is The initial serial number of PC2 is increased by 1. This completes the three-way handshake and establishes a TCP connection between PC1 and PC2.

4. Four waves of TCP connection termination

Detailed explanation of TCPs three-way handshake and four-way wave

At this time, PC1 is CentOS, and the corresponding IP address is 192.168.90.40. PC2 is a real machine, and the corresponding IP address is 192.168.90.10. Analyze his disconnection by capturing packets. The process is as follows:

1. Wave for the first time

Detailed explanation of TCPs three-way handshake and four-way wave

PC1 (server) sends FIN and ACK bits of 1 to PC2 client TCP segment.

2. Wave for the second time

Detailed explanation of TCPs three-way handshake and four-way wave

PC2 client returns a TCP segment with an ACK bit of 1 to the PC1 server.

3. Wave for the third time

Detailed explanation of TCPs three-way handshake and four-way wave

PC2 client sends a TCP segment with FIN and ACK bits of 1 to PC1 server.

4. Wave for the fourth time

Detailed explanation of TCPs three-way handshake and four-way wave

The PC1 server returns a TCP segment with an ACK bit of 1 to the PC2 client, completing the connection termination.

5. The semi-closed concept of TCP four waves (the TCP side stops sending data but can receive it)

1) PC2 client FIN message segment, semi-closed the connection, PC1 server Send ACK segment and receive half-closed.

2) The PC1 server continues to send data, while the PC2 client only sends ACK confirmation and no longer sends any data.

3) When the PC1 server has sent all the data, it sends a FIN segment, and the PC2 client sends an ACK segment, thus closing the TCP connection.

The above is the detailed content of Detailed explanation of TCP's three-way handshake and four-way wave. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to reset tcp/ip protocol in win10? How to reset the tcp/ip protocol stack in windows 10 How to reset tcp/ip protocol in win10? How to reset the tcp/ip protocol stack in windows 10 Mar 16, 2024 am 11:07 AM

How to reset tcp/ip protocol in win10? In fact, the method is very simple. Users can directly enter the command prompt, and then press the ctrl shift enter key combination to perform the operation, or directly execute the reset command to set it up. Let this site do the following. Let us carefully introduce to users how to reset the TCP/IP protocol stack in Windows 10. Method 1 to reset the tcp/ip protocol stack in Windows 10. Administrator permissions 1. We use the shortcut key win R to directly open the run window, then enter cmd and hold down the ctrl shift enter key combination. 2. Or we can directly search for command prompt in the start menu and right-click

How to use TCP to implement conversation between client and server in python How to use TCP to implement conversation between client and server in python May 17, 2023 pm 03:40 PM

TCP client A client sample code that uses the TCP protocol to achieve continuous dialogue: importsocket#Client configuration HOST='localhost'PORT=12345#Create a TCP socket and connect to the server client_socket=socket.socket(socket.AF_INET,socket .SOCK_STREAM)client_socket.connect((HOST,PORT))whileTrue:#Get user input message=input("Please enter the message to be sent:&

See you soon! TCP waves twice, have you seen it? What about the four handshakes? See you soon! TCP waves twice, have you seen it? What about the four handshakes? Jul 24, 2023 pm 05:18 PM

The "connection-oriented" mentioned here means that you need to establish a connection, use the connection, and release the connection. Establishing a connection refers to the well-known TCP three-way handshake. When using a connection, data is transmitted in the form of one send and one confirmation. There is also the release of the connection, which is our common TCP four wave waves.

Using Netty4 for TCP communication in Java API development Using Netty4 for TCP communication in Java API development Jun 17, 2023 pm 11:18 PM

TCP is a type of computer network communication protocol and a connection-oriented transmission protocol. In Java application development, TCP communication is widely used in various scenarios, such as data transmission between client and server, real-time transmission of audio and video, etc. Netty4 is a high-performance, highly scalable, and high-performance network programming framework that can optimize the data exchange process between the server and the client to make it more efficient and reliable. The specific implementation steps of using Netty4 for TCP communication are as follows: Introduction

How to send multiple files using a single TCP connection in Java? How to send multiple files using a single TCP connection in Java? Apr 27, 2023 am 08:49 AM

Why is there this blog about using one TCP connection to send multiple files? I have been reading some related things recently. There is no problem in simply using Socket for programming, but this only establishes some basic concepts. Still nothing can be done about the real problem. When I need to transfer files, I find that I seem to have just sent the data (binary data), but some information about the file is lost (the file extension). And each time I can only use one Socket to send one file, there is no way to send files continuously (because I rely on closing the stream to complete sending files, which means that I actually don’t know the length of the file, so I can only send files as one Socket connection represents a file).

Linux SIGPIPE signal Linux SIGPIPE signal Feb 19, 2024 pm 04:00 PM

Among the TCP communication parties, for the convenience of description, the communication parties are replaced by A and B in the following. According to the TCP protocol, if B continues to send data after A closes the connection, B will receive A's RST response. If B continues to send data, the system will send a SIGPIPE signal to inform that the connection has been disconnected and stop sending. The system's default processing behavior for the SIGPIPE signal is to let process B exit. The default processing behavior of the operating system for the SIGPIPE signal is very unfriendly. Let us analyze it. TCP communication is a full-duplex channel, which is equivalent to two simplex channels, and each end of the connection is responsible for one. When the opposite end "closes", although the intention is to close the entire two channels, the local end only receives the FIN packet. According to the provisions of the TCP protocol, when a

High-concurrency TCP long connection processing skills for swoole development function High-concurrency TCP long connection processing skills for swoole development function Aug 25, 2023 pm 10:01 PM

[Title] Highly concurrent TCP long connection processing techniques for Swoole development functions [Introduction] With the rapid development of the Internet, applications have increasingly higher demands for concurrent processing. As a high-performance network communication engine based on PHP, Swoole provides powerful asynchronous, multi-process, and coroutine capabilities, which greatly improves the concurrent processing capabilities of applications. This article will introduce how to use the Swoole development function to handle high-concurrency TCP long connection processing techniques, and provide detailed explanations with code examples. 【Text】1. Swo

The interviewer asked: How many HTTP requests can a TCP connection send? The interviewer asked: How many HTTP requests can a TCP connection send? Feb 22, 2023 pm 12:00 PM

There was once such a classic interview question: What happens in the process from the URL being entered in the browser to the page being displayed? I believe that most students who have prepared can answer it, but if you continue to ask: If the received HTML contains dozens of image tags, in what way, in what order, how many connections are established, and what protocol are used to download these images? What about?

See all articles