The most complete difference between TCP and UDP [transfer]

Source: https://blog.csdn.net/li_ning_/article/details/52117463


Basic differences between TCP UDP TCP and UDP
  1. Based on connection and no connection
  2. TCP requires more system resources and less UDP;  3. UDP
  program structure is simpler 
  4. Stream mode (TCP) and datagram mode (UDP); 
  5
  .TCP    guarantees   data  correctness, UDP 
  
may   lose
  packets No special requirements   5. The network burden is very heavy, but the response speed is high . Differences in specific programming    1. The parameters of socket() are different     2. UDP Server does not need to call listen and accept     3. Sendto/recvfrom functions are used for UDP sending and receiving data     4 .TCP: The address information is determined when connect/accept     5.UDP: The address information needs to be specified every time in the sendto/recvfrom function     6.UDP: The shutdown function is invalid programming difference    Usually when we talk about network programming, the default refers to TCP programming , that is, use the aforementioned socket function to create a socket for TCP communication, and we usually fill in the function parameter as SOCK_STREAM. That is, socket(PF_INET, SOCK_STREAM, 0), which means that a socket is established for streaming network communication. 




 







 


   The characteristics of SOCK_STREAM are connection-oriented, that is, a connection must be established through connect before sending and receiving data. It is also bidirectional, that is, any party can send and receive data. The protocol itself provides some guarantee mechanisms to ensure that it is reliable and orderly. , that is, each packet arrives at the receiver in the order in which it was sent. 

  And SOCK_DGRAM is the network communication of the User Datagram Protocol. It is connectionless and unreliable, because after the two parties send data, they do not know whether the other party has received the data and whether it has received the data normally. After either party establishes a socket, it can use sendto to send data, or use recvfrom to receive data. It doesn't care whether the other party exists or not at all and whether it has sent data. It is characterized by relatively fast communication speed. Everyone knows that TCP has to go through a three-way handshake, while UDP does not. 

Based on the above differences, the UDP and TCP programming steps are also somewhat different, as follows:

TCP: 
The general steps on the server side of TCP programming are: 
  1. Create a socket, use the function socket(); 
  2. Set the socket properties, use the function setsockopt(); * Optional 
  3. Bind the IP address, port and other information to the socket
  4. To enable monitoring, use the function listen();  5. 
  To receive the connection from the client, use the function accept(); 
  6. To send and receive data, use the functions send() and recv(), or read() and write(); 
  7. Close the network connection; 
  8. Close the monitoring; 

the general steps of the TCP programming client are: 
  1. Create a socket, use the function socket(); 
  2. Set the socket properties, use the function setsockopt( );* Optional 
  3. Bind IP address, port and other information to the socket, use the function bind();* Optional 
  4. Set the attributes such as the IP address and port of the other party to be connected; 
  5. Connect to the server, use the function connect(); 
  6. To send and receive data, use the functions send() and recv(), or read() and write(); 
  7. Close the network connection;

UDP:
The corresponding UDP programming steps are much simpler, as follows: 
  The general steps on the server side of UDP programming are: 
  1. Create a socket and use the function socket(); 
  2. Set the socket attribute, use the function setsockopt();* optional 
  3. Bind the IP address, port and other information to the socket, use the function bind(); 
  4. Receive data in a loop, use the function recvfrom(); 
  5. Close Network connection; 

The general steps of UDP programming client are: 
  1. Create a socket, use the function socket(); 
  2. Set the socket properties, use the function setsockopt();* Optional 
  3. Bind the IP address, port and other information to On the socket, use the function bind();* Optional 
  4. Set the other party's IP address and port and other attributes; 
  5. To send data, use the function sendto(); 
  6. Close the network connection;

TCP and UDP are transport in the OSI model protocol in the layer. TCP provides reliable communication transport, while UDP is often used for communication transport that leaves broadcast and detailed control to applications.

UDP Supplement:
   UDP does not provide a complex control mechanism, and uses IP to provide connectionless communication services. And it is a mechanism that sends the data sent by the application to the network as it is when it is received. Even in the case of network congestion, UDP cannot perform traffic control and other behaviors to avoid network congestion. In addition, if a packet is lost during transmission, UDO is not responsible for retransmission. There is no function to correct even when packets arrive out of order. If these detailed controls are required, they have to be handled by the application using UDO. In other words, UDP transfers some control to the application to handle, but only provides the most basic functions as a transport layer protocol. UDP is somewhat similar to the mechanism that users listen to what they say, but users need to fully consider the type of upper-layer protocol and create corresponding applications.

TCP additions:
  TCP fully realizes various control functions during data transmission, can perform retransmission control of lost packets, and can also control the sequence of packets that are out of order. And none of these are available in UDP. In addition, as a connection-oriented protocol, TCP will only send data when it is confirmed that the communication peer exists, so that the waste of communication traffic can be controlled. TCP achieves reliable transmission through mechanisms such as checksum, sequence number, acknowledgment, retransmission control, connection management, and window control.


Summary of the differences between TCP and UDP:
1. TCP is connection-oriented (such as dialing up to establish a connection to make a call); UDP is connectionless, that is, it does not need to establish a connection before sending data.
2. TCP provides reliable services.
That is to say, the data transmitted through the TCP connection is error-free, not lost, not repeated, and arrives in sequence; UDP does its best to deliver, that is, it does not guarantee reliable delivery Think of it as a series of unstructured byte streams; UDP is a message-oriented
  UDP without congestion control, so network congestion will not reduce the source host's sending rate (useful for real-time applications, such as IP telephony, real-time video conferencing, etc.)
4. Each TCP connection can only be point-to-point; UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication
5. TCP header overhead is 20 bytes; UDP header overhead is small, only 8 bytes
6. The logical communication channel of TCP is a full-duplex reliable channel, and UDP is an unreliable channel

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324708082&siteId=291194637