TCP/IP Detailed Volume One learning Note (iv): TCP connection establishment and termination

Source: Internet
Author: User
Tags ack valid port number

Directory:

TCP Header

Maximum message segment length

Semi-closed

State transition Diagram

Incoming connection request queue

1.TCP Header

TCP provides a connection-oriented, reliable byte-stream service.

Connection-oriented means that two applications that use TCP (typically a customer and a server) must establish a TCP connection before exchanging data with each other.

TCP provides reliability in the following ways: fragmentation. The application data is split into the data block that TCP considers most suitable for sending, which is completely different from UDP, and the datagram length generated by the application will remain unchanged. The unit of information that is passed to the IP by TCP is called a message segment or segment (segment). TCP will control the length of the message segment. Timed retransmission. When TCP sends out a segment, it initiates a timer, waiting for the destination to acknowledge receipt of the message segment. If a confirmation cannot be received in time, the message segment will be re-sent. Receive data acknowledgement. When TCP receives data from the other end of the TCP connection, it sends an acknowledgment. This acknowledgement is not sent immediately and will typically be deferred for a fraction of a second. The first and the data of the inspection and. This is an end-to-end test and is designed to detect any changes in the data during transmission. If the test and errors are received, TCP discards the segment and does not acknowledge receipt of this segment (expecting the originator to timeout and re-send). Re-order. Since TCP packets are transmitted as IP datagrams, and the arrival of IP datagrams can be out of order, the arrival of the TCP message segment may also be out of sequence. If necessary, TCP will reorder the received data and hand the received data to the application tier in the correct order. Discard duplicate data. Since IP datagrams are duplicated, the receiving side of TCP must discard duplicate data. Flow control. Each side of a TCP connection has a fixed-size buffer space. The receiving side of TCP only allows the other end to send the data that the receiving buffer can accept. This prevents faster hosts from causing buffer overruns for slower hosts.

TCP does not explain the content of the byte stream. TCP does not know whether the transmitted data byte stream is binary or ASCII or other type of data. The interpretation of the byte stream is explained by the layer of TCP connections between the two sides.

IP Datagram Format


The port number of the source and destination to find the originator and the end application process. These two values, together with the source-side IP address and destination IP address in the IP header, uniquely determine a TCP connection. Sequence number: Used to identify the data stream sent from the TCP originator to the TCP receiver, which represents the first byte of data in this segment of a packet. Confirm ordinal: Contains the next ordinal number expected to be received at the end of the send acknowledgement. Confirm that the serial number should be the last time the data byte sequence number plus 1 has been successfully received. Only the ACK flag is 1 o'clock to confirm that the ordinal field is valid. 4-bit header length: Refers to the number of 32 bits (4 bytes) in the TCP header (including the Option field). Typically 5 (20 bytes).
6 Flag bits: Multiple of them can be set to 1 at the same time.

URG Emergency pointer (u rgent pointer) is effective. ACK Confirmation number is valid. PSH notifies the receiving party that the received data should be submitted to the receiving process as soon as possible. RST re-establishes the connection. SYN synchronous sequence number, used to initiate a connection. FIN completes the send task and terminates a connection. Window size: Used to provide TCP traffic control. The value that starts at the confirmation ordinal field, which is the byte that receives the correct expected receive. The window size is a single-bit field. Test and: Covers the entire TCP packet segment: TCP header and TCP data. Similar to the UDP calculation, contains a pseudo-header. Emergency pointer: Only the identity bit URG 1 o'clock is valid. It is a positive offset, and the sum of the values in the Ordinal field represents the ordinal of the last byte of the emergency data. Optional fields: The most common optional fields are the longest message size, also known as MSS (Maximum Segment size). Each connector typically indicates this option in the first message segment of the communication (the segment that sets the SYN flag for establishing the connection). It indicates the maximum length of the message segment that can be received on this side.

"Three-time handshake" to establish a TCP connection

1) The requester side (often called the customer) sends a SYN segment that indicates the port of the server to which the customer intends to connect, and the initial sequence number (ISN). This SYN field is message Segment 1.

2) The server sends back the SYN segment (message segment 2) that contains the initial sequence number of the server as a response. At the same time, the confirmation number is set to customer's ISN plus 1 to confirm the customer's SYN message segment. A SYN will occupy an ordinal number.

3) The customer must set the confirmation number to the server's ISN plus 1 to confirm the SYN message segment of the server (message Segment 3).

These three segments complete the establishment of a connection. This process is also known as a three-time handshake (three-way handshake).

When one end sends its SYN for a connection, it selects an initial ordinal for the connection. Isn varies over time, so each connection will have a different isn.

Disconnect the "four-time handshake" from the TCP connection

1) The active shut-down party (client) sends a terminator fin to the server. As with Syn, a fin will occupy a sequence number.
2) When the server receives this fin, it sends back an ACK, confirming that the serial number is the received sequence number plus 1 (message Segment 5). The TCP server also sends a file terminator EOF to the application (that is, the drop server).
3) Then the server program closes its connection, causing its TCP side to send a fin (message segment 6).
4) The customer must send back a confirmation and set the confirmation number to receive the serial number plus 1 (message segment 7).

The TCP connection is full-duplex (that is, the data can be delivered simultaneously in two directions), so each direction must be closed separately. When a party completes its data sending task, it can send a fin to terminate the connection.

TCP disconnects four times because of a handshake:

1) When one end receives a FIN, it must notify the other end of the application layer that the data transfer in that direction has been terminated. Sending fin is usually the result of the application layer closing.
2) A TCP connection can still send data after receiving a fin. This is possible with semi-closed applications.

2.TCP the semi-close

TCP provides the ability for one end of a connection to receive data from the other end after it has been sent. This is called semi-closing.

If the application calls shutdown without calling Close, and the 2nd parameter value is 1, the socket's API supports semi-shutdown.


The left side of the client starts half closed, and of course it can start at the other end. The initial end of Fin, followed by the other end of the fin on the ACK message segment. Because the party receiving the semi-shutdown can still send the data. When a half-closed end is received after it has completed its data transfer, it sends a fin-close connection in this direction, which transmits a file terminator to the application process that initiated the semi-shutdown. When the second fin is confirmed, the connection is completely closed.


3. Maximum message segment length

The maximum segment Length (MSS) indicates the length of the maximum block data transmitted to the other end by TCP.

When a connection is established, both sides of the connection are notified of their respective MSS. If one party does not receive the MSS value from the other, MSS will default to 536 bytes (this default allows a 20-byte IP header and a 20-byte TCP header to fit the 576-byte IP datagram).

When TCP sends a SYN, either because a local application process wants to initiate a connection, or because the host on the other side receives a connection request, it can set the MSS value to the MTU length on the egress interface minus the fixed IP header and the TCP head degree. For an Ethernet, the MSS value can be up to 1460 bytes.

If the destination IP address is non-local (nonlocal), MSS typically has a default value of 536. Whether the address is local or non-local is simple, if the destination IP address of the network number and subnet number is the same as ours, is local, if the destination IP address of the network number is completely different from ours, is not local, if the destination IP address of the network number is the same as ours and the subnet number is different from ours, may be local or non-local.

The purpose of the TCP protocol setting MSS is to limit the length of the TCP datagram to avoid being fragmented by the IP layer over the MTU.


4.TCP state transition Diagram

Status when TCP connections are established and terminated:

time_wait Status

Also called 2MSL wait state. Each specific TCP implementation must select a message segment Maximum lifetime MSL (Maximum Segment Lifetime). It is the maximum time that any message segment is discarded before it is in the network. This time is limited because the TCP segment is transmitted over the network with an IP datagram and the IP datagram has a TTL field that restricts its lifetime.

For a specific implementation of the MSL value given, the principle of processing is: When TCP performs an active shutdown, and then returns the last ACK, the connection must stay in the TIME_WAIT state for twice times the MSL time. Stayed 2MSL time results:

1) Allow TCP to send the final ACK again in case the ACK is lost (the other end times out and the last fin is sent again).

2) During 2MSL Wait, the client IP address and port number of this connection, the IP address and port number of the server can no longer be used. Any late segment of the message will be discarded. Therefore, an earlier, late message segment from the connection cannot be established as part of a new connection.

Also open

It is possible for two applications to actively open each other at the same time, albeit with minimal likelihood of occurring. Each party must send a SYN, and these SYN must be passed to each other. This requires each party to use a port that is known to each other as a local port. This is also known as opening at the same time (simultaneous open). TCP is intentionally designed in order to be able to handle simultaneous opening, for simultaneous opening it establishes only one connection instead of two connections.

also close

It is also possible for both parties to perform a proactive shutdown, and the TCP protocol allows this to be turned off simultaneously.

5. Incoming connection request queue

A concurrent server calls a new process to process each client request, so the server in the passive connection request should always be ready to process the next incoming connection request. However, it is still possible to reach multiple connection requests when the server is creating a new process, or when the operating system is busy processing the higher-priority processes. When the server is busy, TCP is how to handle these incoming connection requests. The following rules are used in the TCP implementation of Berkeley:

1) Waiting for one end of the connection request to have a fixed-length connection queue in which the connection has been accepted by TCP (that is, three handshake has been completed), but not yet accepted by the application layer.

Note The distinction: TCP accepts a connection by placing it in this queue, and the application layer accepts the connection to move it out of the queue.

2) The application layer will indicate the maximum length of the queue, which is often referred to as the backlog value. Its value range is an integer between 0~5, including 0 and 5 (most applications describe this value as 5).

3) When a connection request (that is, SYN) arrives, TCP uses an algorithm that determines whether to receive the connection based on the number of connections in the current connection queue.

Note: The backlog value indicates the maximum number of connections that the TCP listener's endpoint has been accepted by TCP and waits for the application tier to accept. This backlog has no effect on the maximum number of connections allowed by the system, or on the number of concurrent servers that can be processed concurrently.

4) If there is still space in the connection queue for the endpoint of the TCP listener, TCP confirms the SYN and completes the connection establishment.

When the client process actively opens successfully but the server's application layer is unaware of the new connection, it may assume that the server process is ready to receive data (in which case TCP only puts the received data into the buffer queue).

5) If there is no space in the connection queue, TCP ignores the received SYN and does not send back any message segments (that is, it does not send back the RST).

If the application tier cannot accept connections that have been accepted by TCP in a timely manner, these connections may fill the entire connection queue, and the client's active opening will eventually time out.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.