<> Five , Transport layer .

The network layer only sends packets to the destination host , But the real communication is not the host, but the process in the host . The transport layer provides logical communication between processes , The transport layer shields the core details of the network layer below from the high-level users , Make the application look like there is an end-to-end logical communication channel between two transport layer entities .
UDP and TCP Characteristics of :

* User datagram protocol UDP(User Datagram Protocol)
: No connection ; Best effort delivery ; Message oriented ; No congestion control ; Support one-on-one , One to many , Many to one , Many to many interactive communication ; The first one costs little ( There are only four fields : Source port , Destination port , length , Test and ).
* Transmission control protocol TCP(Transmission Control Protocol)
: Connection oriented ; every last TCP Connections can only be point-to-point ( one-on-one ); Provide reliable delivery service ; Provide full duplex communication ; Byte stream oriented .
UDP The header format of :

The first field has only 8 Bytes , Include source port , Destination port , length , Test and .12
The pseudo header of a byte is for the purpose of computation, verification and temporary addition .IP Inspection and only inspection of datagram IP Header of datagram , however UDP The first part and the data part are tested together .
TCP Header format of message segment :

Source port : Each takes two bytes ; Write the source port number and destination port number respectively .
Serial number : occupy 4 Bytes ; Used to number byte streams , For example, the serial number is 301, Indicates that the number of the first byte is 301, If the data length is 100 byte , Then the sequence number of the next message segment should be
401.
Confirmation number : occupy 4 Bytes ; The sequence number of the next segment expected to be received . for example B Received correctly A A segment of a message sent , The serial number is 501, The length of the data carried is 200 byte , therefore B
The sequence number of the next message segment is expected to be 701,B Send to A In the confirmation message segment, the confirmation number is 701.
Data offset : occupy 4 position ; The offset of the data segment at the beginning of the message , It's actually the length of the head .
confirm ACK : When ACK=1 The confirmation number field is valid , Otherwise, it is invalid .TCP regulations , After the connection is established, all transmitted segments must be ACK Set up 1.
synchronization SYN : Used to synchronize sequence numbers when a connection is established . When SYN=1,ACK=0 This is a connection request message segment . If the other party agrees to establish a connection , In the response message SYN=1,ACK=1.
termination FIN : Used to release a connection , When FIN=1 Time , Indicates that the data of the sender of this message segment has been sent , And ask to release the connection .
window : occupy 2 byte ; The window value serves as the basis for the receiver to let the sender set its sending window . The reason for this limitation , Because the data cache space of the receiver is limited .
Test and : occupy 2 Bytes ; The scope of inspection and field inspection includes header and data . When calculating the check sum , stay TCP Before the message segment plus 12 Pseudo header of byte .
socket :TCP A socket, or socket, connection . Port number spliced to IP The address makes up the socket .
* Representation of socket :
In decimal IP Write the port number after the address , The middle is separated by a colon .
every last TCP Connect two endpoints that are unique to each other ( Two sockets ) Determined .
TCP connect ::={socket1,socket2}={(IP1:port1),(IP2,poet2)}
be careful : The same IP There can be multiple different addresses TCP connect ; The same port number can also appear in multiple different TCP Connecting .
Port number :
*
The port number of the transport layer is divided into the port number used by the server (0——1023 Assign to well-known ports ,1024——49151 Is the registration port number ) And the port number temporarily used by the client (49152——65535)
TCP Three handshakes :

hypothesis A For client ,B Server side .
* first B be in LISTEN( monitor ) state , Waiting for a connection request from the customer .
* A towards B Send connection request message ,SYN=1,ACK=0, Select an initial sequence number x.
* B Receive connection request message , If you agree to establish a connection , Then to A Send connection confirmation message ,SYN=1,ACK=1, The confirmation number is x+1, Also select an initial sequence number y.
* A received B After the connection confirmation message of , But also to the B Issue confirmation , The confirmation number is y+1, The serial number is x+1.
* B received A After confirmation of , Connection establishment .
Reasons for three handshakes :

The third handshake is to prevent invalid connection requests from reaching the server , Let the server open the connection incorrectly . If the connection request sent by the client is stranded in the network , Then it will take a long time to receive the connection confirmation from the server . After the client waits for a timeout retransmission time , The connection will be re requested . But this stranded connection request will eventually arrive at the server , If you don't shake hands three times , Then the server opens two connections . If there's a third handshake , The client ignores the connection confirmation sent by the server later on for the stranded connection request , No third handshake , So the connection is not opened again .
TCP Four wave process :

notes : Serial number and confirmation number are not discussed in the following description , Because the rules of serial number and confirmation number are relatively simple . And don't discuss it ACK, because ACK After the connection is established 1.
* A Send connection release message ,FIN=1.
* B Send confirmation when received , here TCP It belongs to semi closed state ,B To be able to A Send data but A Can't go to B send data .
* When B When the connection is no longer needed , Send connection release message ,FIN=1.
* A Send confirmation when received , get into TIME-WAIT state , wait for 2 MSL( Maximum message lifetime ) Post release connection .
* B received A Release the connection after confirmation .
The reason for four waves :
The client sent FIN After connection release message , The server received this message , And they entered CLOSE-WAIT
state . This state is to allow the server to send data that has not yet been transmitted , After transmission , The server will send FIN Connection release message .
Time waiting timer :(TIME_WAIT)
The client received the FIN Enter this status message , This is not a direct entry CLOSED state , You also need to wait for a time timer to set the time 2MSL. There are two reasons for this :
* Ensure that the last acknowledgement message arrives . If B Not received A Confirmation message sent , The connection release request message is sent again ,A Waiting for a while is to deal with this .
* Waiting for a period of time is to make all messages generated during the duration of this connection disappear from the network , So that the next new connection will not appear the old connection request message .

Technology