TCP/IP Detailed study notes-17th 18 TCP: Overview, connection establishment and termination

Source: Internet
Author: User
Tags ack bit set

1. Overview

TCP provides a connection-oriented, reliable byte-stream service. Full-duplex communication. A TCP connection is uniquely determined by a 4-tuple: Local IP address, local port number, remote IP address, and remote port number.

TCP packs the user data into a message segment, initiates a timer when it sends the data, confirms the received data on the other end, re- sorts the unordered data, discards duplicate data, and TCP provides an end-to-end flow control, and calculate and validate a mandatory end-to-end inspection and .
Many popular applications such as Telnet, Rlogin, and ftp,smtp all use TCP.

TCP provides reliability in the following ways:
(1) The application data is divided into the data blocks that TCP considers most suitable for sending. This is completely different from UDP, and the datagram length that the application produces will remain unchanged. The unit of information that is passed to the IP by TCP is called a message segment or segment (segment).
(2) When TCP sends 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.
(3) 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.
(4) TCP will keep its header and data checked and. This is an end-to-end test and is designed to detect any changes in the data during transmission. If the test and error of the segment are received, T p will discard this segment and not acknowledge receipt of this message segment (expecting the originator to timeout and re-send).
(5) Since the TCP packet is transmitted as an IP datagram, and the arrival of the IP datagram may 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.
(6) Since the I P datagram is duplicated, the receiving side of TCP must discard duplicate data.
(7) TCP can also provide 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.

Summary:the way to maintain reliability in TCP is to re-send overtime. If you do not get confirmation, resend the datagram until you get confirmation from the other party.

2. TCP Header

The TCP data is encapsulated in an IP datagram,

The data format for the TCP header. If the optional field is not counted, it is usually 20 bytes .

(1) Source port and destination port: Used to find the originator and the end application process. These two values add
The source-side I-p address and the destination I P-address of the upper I P header uniquely determine a T-C-P connection. (2) interface, Transport layer reuse and decomposition functions are to be achieved through the port.
(3) Serial number: Identifies the data byte stream sent from the T C P originator to the T-C P receiver. Occupies 4 bytes, and the ordinal range is [0,2^32-1].
(4) Confirmation number: The first byte ordinal of the data that is expected to receive the next message segment of the other. If the confirmation number = = N, it indicates that all data until the ordinal N-1 has been received correctly.
(5) Header length: Maximum 60 bytes. There is no optional field, and the normal length is 20 bytes.
(6) Retention: 6bit, all 0.
(7) There are 6 flag bits in the TCP header. Multiple of them can be set to 1 at the same time. Emergency Urg, confirm bit ack, push bit PSH, reset bit (RST), synchronous bit SYN, terminate bit fin.
(8) window: Used to control the amount of data sent by each other. The traffic control for TCP is provided by the window size declared by each end of the connection. The unit is in bytes. One end of a TCP connection determines its own receive window size based on the size of the cache space set. The notification is then issued with the defined Maximum Send window. The value of the window is often in dynamic change
(9) Testing and: testing and covering the entire TCP packet segment: TCP header and TCP data. This is a mandatory field that must be computed and stored by the originator and validated by the end of the collection.
(10) Emergency pointer: The emergency pointer is valid at Urg=1, which indicates the number of bytes of emergency data in this section.
(11) Option: variable length, up to 40 bytes

3, the establishment of TCP connection

Set Host B to run a server process, which first issues a passive open command that tells it that TCP is ready to receive successive requests from the client process, and then the service process is in a listening state. Continuously detect if there are client processes initiating continuous requests, if any, to respond. The client process runs in host A, and he first sends a proactive command to his own TCP, indicating that to establish a transport connection to a port on one of the IP addresses, the three-time handshake process is as follows:

1)主机A的TCP向主机B的TCP发出连接请求报文段,其首部中的同步比特SYN应置1,同时选择一个序号x,表明在后面传送数据时的第一个数据字节的序号是x。2)主机B的TCP收到连接请求报文段后,如同意,则发送确认。在确认报文段中应将SYN置为1,确认号应为x+1,同时也为自己选择一个序号y3)主机A的TCP收到此报文段后,还要向B给出确认,其确认号为y+14)主机A的TCP通知上层应用进程,连接已经建立,当主机B的TCP收到主机A

three handshake When a connection is established, it is necessary for the sender to send the acknowledgment again:
The main purpose is to prevent the failed connection request message segment from suddenly being transmitted to B, resulting in an error . Suppose that an exception occurred, that is, the first connection request packet sent by a segment is not lost, but in some network nodes for a long time, has been delayed until the connection is released some time after the arrival of B, This is a long overdue message segment . However, after receiving this invalid connection request packet, B is mistaken for a and a new connection request, so send a confirmation message to a section, agree to establish a connection. assuming that the three-time handshake is not used, the new connection is established as soon as B confirms, so that a lot of resources for B are wasted.

4. Termination of TCP connection

After the data transfer is complete, both sides of the communication can issue a disconnect request. The process for disconnecting is as follows:

1After the data transfer is complete, the hostAApplication process first sends a disconnect request to its TCP and no longer sends the data. TCP notifies the other party to release theATo a connection to B, the terminating bit of the header of the TCP segment destined for Host B is1, the ordinal u equals the ordinal of the last byte of the transmitted data plus1。2) Host B's TCP receives a release connection notification after the acknowledgement is issued with a sequence number of u+1, and notifies the application process thatAThe connection to B is released and the connection is in a semi-closed state. Host B no longer accepts hostsABut Host B also sends the data toASend data, HostAIf you receive data correctly, you still need to send confirmation.3) in Host B to the hostAAfter the data is sent, its application process notifies TCP to release the connection. The connection release message segment issued by Host B must have the terminating bit set to1and makes its ordinal w equal to the ordinal of the last byte of the data previously passed1, you must also repeat the last ack=u+ you sent1。4) hostAConfirm the connection release message segment of Host B and set the ACK to1, ack=w+1, seq=u+1。 So we can get from B toAThe reverse direction of the connection is released, the hostATCP to its Application process report, the entire connection has been released.

four times to wave and release the connection, wait for the meaning of 2MSL:
(1) In order to ensure that a send the most one ACK message segment can reach B. This ACK segment may be lost so that B in the Last-ack state does not receive confirmation of the sent fin and ACK segments. B will time out to retransmit the fin and ACK segment, and a will receive the retransmission Ack+fin message segment within 2MSL time. Then a re-transmit confirmation.
(2) To prevent the above mentioned invalid connection request message segment appears in this connection, a after sending out the most one ACK segment, then after 2MSL, you can make the connection for the duration of the time generated by all the message segments from the network disappear.

The state of the client can be represented by the following process:

CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAIT->CLOSED

The status of the server can be represented by the following process:

CLOSED->LISTEN->SYN收到->ESTABLISHED->CLOSE_WAIT->LAST_ACK->CLOSED

fin_wait_2 Status:
The well-known semi-closed state, which is the state after the client and server two-time handshake when the connection is closed . In this state, the application also has the ability to accept data, but it has been unable to send data , but it is also possible that the client has been in a fin_wait_2 state, and the server has been in the Wait_close state, and until the application layer decides to close the state.

TCP/IP Detailed study notes-17th 18 TCP: Overview, connection establishment and termination

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.