The difference between TCP protocol and UDP protocol and the connection with TCP/IP protocol

First introduce what is TCP and what is UDP.

1. What is TCP?

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable byte stream service, that is, before sending and receiving data, a reliable connection must be established with the other party. This process is similar to making a call: first dial the phone and ring, wait for the other party to answer the call, say hello, and then say who you are. In the Internet protocol suite (Internet protocol suite), the TCP layer is the transport layer above the IP layer and below the application layer.


2. What is UDP?

UDP (User Datagram Protocol, User Datagram Protocol) is a non-connection transport layer protocol in the OSI (Open System Interconnection) reference model. It provides transaction-oriented simple and unreliable information transmission services. Before data transmission The source and the terminal do not establish a connection. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible, so it is not safe.


3. The three-way handshake process of TCP?

1. Host A sends a data segment containing the flag bit of the synchronization sequence number to host B by sending a data segment to host B, and requests to host B to establish a connection. Through this data segment, host A tells host B two things: I want to be with you Communication; which serial number you can use as the starting data segment to respond to me.

2. After receiving the request of host A, host B responds to host A with a data segment with acknowledgement (ACK) and synchronization sequence number (SYN) flag bits, and also tells host A two things: One is: I have After receiving your request, you can transmit data;

  The second is: Which serial number do you want to use as the starting data segment to respond to me;

3. After host A receives this data segment, it sends a confirmation response to confirm that it has received the data segment of host B: "I have received the response, and I want to start transmitting actual data now";

In this way, the 3-way handshake is completed, and host A and host B can transmit data.

Features of the 3-way handshake:

  • No application layer data;

  • The SYN flag will be set to 1 only when the TCP connection is established;

  • The SYN flag is set to 0 after the handshake is completed;

4. The four waves of TCP?

TCP connection establishment requires 3 handshake, while disconnection requires 4 times

1. When host A completes the data transmission, it sets the control bit FIN to 1, and requests to stop the TCP connection; 2. Host B responds to it after receiving FIN, confirms that the TCP connection in this direction will be closed, and sets ACK to 1; 3. The B-side requests for closing in the opposite direction again, and FIN is set to 1; 4. Host A confirms the request of host B and sets ACK to 1, and the two-way closing ends;


  • ACK One of the control bits of the TCP header to confirm the data. The confirmation is sent by the destination, and it is used to tell the sender that the data segment before the sequence number has been received. For example, if the confirmation number is X, it means that the first X-1 data segments have been received. Only when ACK=1, the confirmation number is valid. When ACK=0, the confirmation number is invalid. At this time, the data will be retransmitted. Ensure data integrity;

  • SYN synchronization sequence number, this bit is set to 1 when TCP establishes a connection;

  • FIN The sender completes the sending task bit. When TCP completes data transmission and needs to be disconnected, the party that proposes to disconnect will set this bit to 1.

It can be seen from the three-way handshake and four-time disconnection of TCP:

TCP uses a connection-oriented communication method, which greatly improves the reliability of data communication, so that the sender and receiver can interact before the data is officially transmitted, and lay a reliable foundation for the formal data transmission.

More exciting content for C/C++Linux server development includes: C/C++, Linux, Nginx, ZeroMQ, MySQL, Redis, MongoDB, ZK, streaming media, P2P, Linux kernel, Docker, TCP/IP, coroutine, DPDK Sharing of multiple advanced knowledge points. Click the link to subscribe and watch it directly: C/C++Linux server development-learning video

Video learning materials acquisition + group 720209036

5. What is the difference between TCP and UDP?

1. TCP is based on connection, while UDP is based on connectionless;

2. Requirements for system resources: TCP is more (TCP has 20 bytes of information packets), UDP is less (UDP information packets are only 8 bytes);

3. The UDP program structure is relatively simple;

4. TCP is a byte stream mode, while UDP is a data message mode;

5. TCP guarantees data correctness, safety and reliability, and guarantees data sequence, while UDP may lose packets, and UDP does not guarantee data sequence.

6. The following briefly talk about the differences between TCP, UDP and TCP/IP protocols:

The TCP/IP protocol is a protocol cluster. It includes many agreements. UDP is just one of them. It is named TCP/IP protocol because TCP and IP protocol are two very important protocols, so they are named after the two.

The TCP/IP protocol set includes the application layer, transport layer, network layer, and network access layer.

The application layer includes:

  • Hypertext Transfer Protocol (HTTP): The basic protocol of the World Wide Web.

  • File transfer (TFTP Simple File Transfer Protocol):

  • Telnet, which provides remote access to other hosts, allows users to log in

  • internet host, and execute commands on this host.

  • Network management (SNMP Simple Network Management Protocol), which provides methods for monitoring network devices, as well as configuration management, statistical information collection, performance management, and security management.

  • Domain Name System (DNS), which is used to convert domain names and their public broadcast network nodes into IP addresses in the internet.

The network layer includes:

  • Internet Protocol (IP)

  • Internet Control Message Protocol (ICMP)

  • Address Resolution Protocol (ARP)

  • Reverse Address Resolution Protocol (RARP)

Network access layer:

Also known as the host-to-network layer (host-to-network).

The functions of the network access layer include:

  • Mapping of IP address and physical address hardware;

  • Encapsulate IP into frames.

Based on network interfaces of different hardware types, the network access layer defines the connection with the physical medium.

The following is a model diagram of the protocol layer from bottom to top:

And the corresponding functions of each layer:

There are protocols used in the application layer, presentation layer and session layer:

Guess you like