In-depth understanding of computer networks-10 Transport Layer 4

Table of contents

1. Overview of TCP protocol

2. TCP data segment format

3. TCP socket

4. TCP port


1. Overview of TCP protocol

 The main transport layer protocol in the TCP/IP protocol architecture, which is different from OSI/RM.

Main features:

(1) Connection-oriented transmission protocol;

(2) Only unicast transmission is supported, multicast and broadcast are not supported. Each TCP transmission connection can only perform endpoint-to-endpoint transmission (the endpoint refers to the socket, which consists of IP and port number, such as (IP, PORT) or {socket1, socket2} or {(IP1, Port1), ( IP2,Port2)}).

(3) Provide reliable delivery services.

(4) The transmission unit is data segment.

The size of the data segment is determined by the size of the message transmitted by the application layer and the size of the MTU value in the network it passes through, so the size of the TCP data segment sent each time is not fixed. In a specific network, there is an MSS (Maximum Segment Size, maximum data segment size), and the minimum data segment may be only 21 bytes (20 bytes of which belong to the TCP data segment header, and the data part is only 1 byte )

(5) Only one TPDU format.

It will not be like there are more than 10 different formats of TPDU in OSI/RM. Because the TCP data segment header already includes the characteristic fields required by various TPDUs (implemented through multiple control bits)

(6) Support full-duplex transmission. Both communicating parties can send and receive data.

(7) TCP connections are based on byte streams rather than message streams

(8) The size of the TCP data segment and the number of data ends sent each time are variable.

It is decided based on the window size given by the other party and the current network congestion level.

2. TCP data segment format

Two factors determine the size of the data segment:

First, the size of each TCP data segment must meet the 65515-byte payload limit of the IP datagram. The maximum size of an IP datagram cannot exceed 65535 bytes, which is 64KB, and the minimum header is 20 bytes.

Second, each network has an MTU value (actually the size of the data link layer frame), so each TCP data segment must fit within the MTU limit.

(1) Source port source prot and destination port destination port

Constitute a socket with the host IP

(2) sequence number

In a TCP connection, each data byte in the transmitted data byte stream must be numbered in sequence, and the starting sequence number of the entire byte stream to be transmitted must be set when the connection is established .

(3) confirmation number acknowledgment number

Refers to the first byte sequence number expected to be received in the "data" part of the other party's next data segment.

It means that all the data before this value has been continuously and correctly received by the other party (not the sequence number that represents the last byte has been correctly received), because there may be one or more data between two correctly received data segments. Not received correctly.

For example: Host B correctly receives 101 and 201 and then also receives 501 and 601 data segments. The confirmation number of the returned ACK data segment should be 301, not 501 and 601, because the middle 301 and 401 have not been received correctly.

(4) Data offset

Also called "TCP header length", because in addition to the fixed 20 bytes, there are optional options (up to 40 bytes), occupying 4 bits (the maximum represents 1111, which is 15), in 4-byte units, the maximum header is 15 *4=60 bytes.

(5) Reserved, all 0

(6) URG emergency pointer control bit

Indicate at the data sending end whether the currently sent data segment contains urgent data (i.e. 1). Urgent data will be sent first and will not be sent in queue order. When this field is set to 1, the following emergency pointers are meaningful.

(7)ACK

Acknowledgment control bit indicates whether the confirmation number field in the TCP data segment is valid. 1 means valid, and the application layer entity can ignore the confirmation number field when reading data.

(8)PSH

Push control bit is used to indicate whether the receiving end needs to immediately submit the received data segment to the application system.

(9)RST

Rest reset control bit is used to reset and release a chaotic transmission connection, and then reestablish a new transmission connection. When it is 1, the current connection is released, and then a new transport connection can be re-established.

(10)SYN

Synchronization synchronization control bit is used to synchronize the transmission connection sequence number when the transmission connection is established. When 1, it indicates that this is a connection request or connection confirmation message. When SYN=1 and ACK=0, it indicates that a connection is requesting a data segment. If the other party agrees to establish the connection, the other party will return a confirmation of SYN=1, ACK=1.

(11)FIN

Final The last control bit is used to release a transport connection. When FIN=1, it means that all data transmission has been completed.

When the sender has no data to transmit, it can request to release the current connection, but the receiver can still continue to receive unfinished data. Normal FIN=0.

(12) Window size

Indicates the window size used to store incoming data segments on the host sending this TCP data segment, that is, the maximum number of bytes that the sender can currently receive.

The value of the window size field tells the host receiving this data segment that, starting from the "confirmation number" value set in this data segment, the number of bytes that this end currently allows the other end to send is to allow the other end to set its sending window size. Basis (how many buffers do I have and how much data can you send)

For example: the "confirmation number" field value of the data segment sent this time is 501, and the "window size" field value of the data segment is 100, then starting from 501, the local end can receive 100 bytes.

(13) Checksum Checksum

(14) Urgent Pointer

It is only meaningful when the previous URG=1, which indicates the number of bytes of urgent data in this data segment. Even if the current window size is 0, class data can be sent because urgent data does not need to be cached.

(15)Option Option

Optional options include: Wsopt=window scale option, MSS (maximum data segment size) option, SACK (selection confirmation) option, timestamp (Timestamp) option, etc.

Each TCP data segment size must comply with the IP packet payload size limit of 65535 bytes, with a maximum of 65535-20 = 65515 bytes.

(16) DataData

3. TCP socket

Socket cannot be equated to TSAP, it is just NVC. In fact, Socket only includes the TASP address-the transport layer port.

In TCP/IP, there are three main parameters to distinguish network communication and connections between different application processes:

Communication destination IP address, transport layer protocol used (TCP/UDP), and port number used.

The three parameters are combined and bound to a Socket to distinguish.

TSAP is located at the upper edge of the transport layer (still belongs to the transport layer), while socket is completely located at the application layer, but it calls the port of the transport layer.

Socket includes the TSAP address and the service primitives of the transport layer.

Server-side primitive execution:

1. The SOCKET primitive call creates an endpoint and returns a file descriptor;

2. There is no network address after creation, and now it needs to be assigned through BIND primitive binding;

3. After binding, call the LISTEN primitive to allocate cache space so that subsequent connections can be queued so that multiple clients can access a server at the same time. At this time, the server is not in a blocking state (that is, it has not yet entered the waiting for connection state);

4. When a TCP segment requesting a connection arrives, a new socket is created by calling the ACCEPT primitive and a file descriptor associated with it is returned. This new socket has the same properties as the original socket created by the SOCKET primitive. At this time, the server can call a process to handle the new connection on this socket, and the server itself goes back to waiting for the next connection on the original socket.

Client primitive call:

1. Create a new socket using the SOCKET primitive. There is no need to bind a network address, that is, the client does not need to call the BIND primitive;

2. Block the client through the CONNECT primitive and actively initiate a TCP connection. That is, after receiving the confirmation data segment from the server, the client enters the unblocked state and establishes a transmission connection.

At this time, both the server and the client can use the SEND and RECV primitives to send and receive data.

The connection will not be truly released until both parties execute the CLOSE primitive.

4. TCP port

A port is an abstract software structure, including some data structures and I/O buffers. It belongs to the category of software interface and is the transport layer TASP address.

(1) Reserved port

0-1023, regular port, recognized port.

(2) Dynamically allocate ports

Port numbers greater than 1024 can be dynamically assigned to applications.

(3) Registration port

It is also a port fixed for a certain application. It is an application developed by a certain software manufacturer. For example, CCProxy's port 8080 is generally larger than 1024.

There are 8 TCP Socket service primitives, fewer than OSI/RM.

The state of the hosts on both sides of the communication during the establishment and release of the TCP transport connection is called "Finite State Machine" Finite State Machine FSM

Box: Indicates the status of the communication host in different periods

Arrows: represent transitions between states

Thick line: indicates the normal process of the client actively establishing a connection with the server.

Among them, the client's state transition is represented by a thick solid line with an arrow .

The server side is represented by a thick dashed line           with an arrow.

Thin solid lines with arrows represent some uncommon events such as reset, simultaneous opening, simultaneous closing, etc.

Guess you like

Origin blog.csdn.net/hongdi/article/details/126263733