Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

TCP three-way handshake and four disconnects

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

TCP, a protocol that everyone is familiar with, for technicians, a thorough understanding of him means that half of our feet have stepped into the door of IT.

Characteristics of TCP

TCP provides a connection-oriented and reliable byte streaming service. Connection-oriented means an one-to-one connection (usually a client to a server) that needs to be established before exchanging data. In TCP connections, only one-to-one parties establish connections, and multicast and broadcasting do not belong to TCP connections.

The Mechanism of TCP ensuring its Reliability

The application data is divided into blocks that TCP considers most suitable for transmission. The unit of information passed from TCP to IP is called a message segment or segment (segment).

Timeout retransmission strategy. When TCP sends out a segment, it starts a timer and waits for the destination to acknowledge receipt of the segment. If a confirmation is not received in time, the message will be re-sent.

When TCP receives data from the other end of the TCP connection, it sends an acknowledgement. This confirmation is not sent immediately and will usually be delayed by a fraction of a second.

TCP will maintain its header and data verification and. This is an end-to-end checksum designed to detect any changes in the data during transmission.

If there is an error in the check of the received segment, TCP will discard the message segment and do not acknowledge the receipt of the message segment (hopefully the sender timed out and resent it).

Since TCP message segments are transmitted as IP datagrams, and the arrival of IP datagrams may be out of order, the arrival of TCP message segments may also be out of order. If necessary, TCP will reorder the received data and deliver the received data to the application layer in the correct order.

Since the IP Datagram is duplicated, the receiver of the TCP must discard the duplicate data.

TCP also provides flow control. Each side of the TCP connection has a fixed size of buffer space. The receiver of TCP only allows the other end to send data that can be accepted by the receiver buffer. This will prevent faster hosts from causing buffer overflows on slower hosts.

(see "TCP/IP detailed unwinding 1")

Some attribute descriptions of TCP

Port number:

Each TCP segment contains the port numbers of the source and destination, which are used to find the originating and receiving application processes. These two values add up.

The source IP address and destination IP address in the IP header uniquely determine a TCP connection.

Network socket (socket):

An IP address and a port number are also known as a socket. It is also used as a programming interface for Berkeley. Socket contains a quad of customer IP address, client number, server IP address, and server port number to uniquely determine the parties to each TCP connection in the Internet.

Full duplex:

TCP provides full-duplex service for the application layer. This means that data can be transmitted independently in both directions. Therefore, even

Each end of the connection must maintain the transmission data sequence number in each direction.

When we use tcpdump in the Linux system, we can see some data transmission information, which contains some header information of the TCP packet:

# tcpdump-I eth0'((host 192.168.1.210) and (port 80)) '13host 30purl 19.078214 IP 192.168.1.106.53190 > 192.168.1.210.http: Flags [S], seq 1725365454, win 8192, options [mss1460,nop,wscale 8Reagin nopjingsackOK], length 0131414078342 IP 192.168.1.210.http > 192.168.1.106.53190: Flags [S.], seq 4163517334, ack 1725365455, win 14600 options Nop,wscale 6], length 013 192.168.1.210.http 30 IP 19.078649 192.168.1.106.53190 > 192.168.1.210.http: Flags [.], ack 1, win 68, length 0

The above is the process of establishing a connection with a three-way handshake.

The format is: original address > destination address: marked, [S] marked SYN, where win represents the window size, that is, the amount of data, and can be used for flow control. The default is 4096, and the maximum is 65535, which is represented by a 16bit field.

SYN: synchronous serial number connection tag, used to initiate a connection.

ACK: the reply mark is used to confirm that the synchronization sequence number is valid.

FIN: end the connection mark.

TCP three-way handshake process

The three-way handshake process of TCP can actually be represented by a simple diagram:

Connection process:

The requester (usually the client) sends a request for a SYN segment, indicating the port of the service the client intends to connect to and the initial serial number ISN, assuming that the message segment is SYN0.

The server sends back the SYN message segment (SYN1) containing the initial sequence number of the server as a reply, and adds 1 to the SYN sent by the requester to confirm it in the way of ACK. The reason for adding 1 is that a SYN will occupy a sequence number.

The client must set the acknowledgement sequence number to the server's ISN plus 1 and return an ACK to confirm the server-side SYN message.

This establishes the connection.

This simple process can be understood as going to the store to buy things.

You make a request to the waiter and say: I want xxx, please give it to me (SYN0)

The waiter said: OK, I have received your request (SYN1) you want xxx, right (ACK=SYN0+1)

You say: yes (Ack)

And then you start trading.

TCP four-time disconnection process

Establishing a connection requires three handshakes, while terminating a connection takes four handshakes. This is caused by the semi-shutdown (HALF-CLOSE) of TCP. Since a TCP connection is full-duplex (that is, data can be passed in both directions), each party

Must be closed separately. The principle is that when one party finishes its data sending task, it can send a FIN to terminate it.

Connect in this direction. When one end receives a FIN, it must notify the other end of the application layer that the data transmission in that direction has been terminated several times.

Send it. Sending a FIN is usually the result of a shutdown at the application layer.

The diagram of the four disconnections is as follows:

The process of disconnecting:

When one party wants to terminate the connection, it will send the other party a FIN signal n.

After receiving the message, the recipient will reply with an ACK (nasty 1) indicating that the request has been received, but will not immediately disconnect the connection, but will try to close its own connection.

When the responding client closes the local TCP connection, a new FIN m is re-sent to the requester, indicating that the responder can shut down.

After receiving the signal from FIN m, the requester replies with an ACK. At the same time, it also enters the TIME_WAIT state, while the response side enters the close state.

Here, a diagram of the TCP/IP protocol volume is applied to illustrate the state change of the host during the TCP interaction:

In fact, these contents are only the tip of the iceberg of TCP protocol, there are many algorithms and protocol rules in this interaction process, you can refer to the TCP/IP protocol volume for details.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report