Author: Not Only Coding, focusing on Java, Internet of Things, cryptography, algorithms and other fields, architect community partner!

Mp.weixin.qq.com/s/_fpZKI8KB…

TCP and UDP are two important protocols at the transport layer

Transmission Control Protocol (TCP)

User Datagram Protocol (UDP) User Datagram Protocol


The main features of TCP:

1. Connection-oriented

2. Each TCP connection can only be point-to-point (one-to-one).

3. Provide reliable delivery services

4. Provide full-duplex communication

5. Byte oriented stream


Main features of UDP:

1. No connection

2. Deliver as best you can

3. Packet oriented

4. No congestion control

5. Support one-to-one, one-to-many, many-to-one and many-to-many interactive communication

6. Small header overhead (only 4 fields: source port, destination port, length, check and)


There are four TCP congestion control algorithms: slow start, congestion avoidance, fast retransmit and fast recovery.


The unit protocol for TCP data transmission is TCP segment.

UDP The transmitted data unit protocol is the UDP message or user datagram.

TCP does not provide broadcast or multicast services.


UDP header format:

Source port: 2 bytes

Destination port: 2 bytes

Length: 2 bytes

Check sum: 2 bytes


Port: The abstract protocol port between the hardware port and the software port in the protocol stack is the software port. Ports on routers or switches are hardware ports. Hardware port is an interface for different hardware devices to interact, while software port is an address for various protocol processes of the application layer to interact with transport entities. To communicate with each other, processes on two machines must know not only the IP address of the other machine (to find the other machine) but also the port number of the other machine (to find the application process on the other machine). Port number used by the server Familiar port number. The value ranges from 0 to 1023. Register the port number. The value ranges from 1024 to 49151 and is used by applications that are not familiar with the port number. Port numbers using this range must be registered with IANA to prevent duplication. The port number used by the client is also called the transient port number. The value ranges from 49152 to 65535 and is reserved for the client process to use temporarily. Familiar port: File Transfer Protocol (FTP) File Transfer Protocol: 21TELNET remote terminal Protocol: 23 Simple Mail Transfer Protocol (SMTP) 25 Domain Name System (DNS) : 53 Trivial File Transfer Protocol (TFTP) : 69 HyperText Transfer Protocol (HTTP) : 80 Simple Network Management (SNMP) Simple Network Management protocol: 161SNMP (trapThe endpoint of a TCP connection is not a host, nor an IP address of the host, nor an application process, nor a protocol port of the transport layer. The end points of a TCP connection are called sockets or sockets. Each TCP connection is uniquely identified by two endpoints (that is, two sockets) at both ends of the communication. TCP connection: : = {socket1, socket2} = {(IP1: port1), (IP2: port2)} reliable transmission principle of work: stop waiting agreement continuous ARQ protocol -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -Copy the code



“Stop waiting” is to stop sending each packet and wait for the confirmation of the other party. Send the next packet after receiving confirmation.

A situation where something goes wrong:

At receiver B, two situations occur:

B detects an error when receiving M1, discards M1 and does nothing else (does not notify A of receiving the faulty packet).

M1 is lost in transit, and of course B doesn’t know anything and doesn’t do anything.

In both cases, B doesn’t send any information.

How do you ensure that B receives M1 correctly?

Solution: Retry timeout

A sets A timeout timer for each packet that is sent.

As long as A receives the corresponding acknowledgement before the timeout timer expires, it will cancel the timeout timer and continue to send the next packet M2.


Automatic Repeat reQuest (ARQ). This means that the retransmission request is automatic and the receiver does not need to ask the sender to retransmit an error packet.


Continuous ARQ protocol:

The sending window maintained by the sender. The meaning of this window is that the packets in the sending window can be sent consecutively without waiting for the confirmation of the other party. In this way, channel utilization is improved.

The continuous ARQ protocol states that the sender slides the send window forward one block for each confirmation received.

TCP transport connection established:

TCP is a connection-oriented protocol.

The transport connection has three stages:

Connection is established

Data transfer

Connection release


The TCP connection establishment process is called three-packet handshake


The TCP of A sends A connection request packet to B. The synchronization bit in the header is SYN = 1 and the serial number is seq = X, indicating that the serial number of the first data byte is X. After receiving the connection request packet, THE TCP of USER B sends an acknowledgement if the TCP of user B agrees the connection request packet. B should set SYN = 1, ACK = 1, ACK = X + 1, and seq = y in the confirmation packet segment. After receiving the packet, USER A sends an ACK = 1 to user B. The ACK number is ACK = Y +1. The TCP of USER A notifies the upper-layer application process that the connection is established. After receiving the confirmation from host A, TCP of USER B also notifies the upper-layer application process that the TCP connection is established. ---------------------Copy the code


After the data transfer ends, the communication parties can release the connection.

The TCP connection release process is a four-packet handshake.

After the data transfer ends, the communication parties can release the connection. Now, the application process of A sends the connection release packet segment to its TCP, stops sending data, and actively closes the TCP connection. A connects to FIN = 1 at the head of the release packet segment and waits for B's confirmation. B sends an acknowledgement with ack = U +1 and the sequence number of the packet is seq = V. The TCP server process notifies the upper-level application process. The connection from A to B is released, and the TCP connection is half-closed. If USER B sends data, User A still receives it. If B has no data to send to A, its application notifies TCP to release the connection.Copy the code