The difference between TCP and DUP


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 . The difference 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 : address information is determined when connect/accept  5.UDP : address information    needs    to be specified every time in sendto/recvfrom function  , 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, which is connectionless and unreliable, because after the two parties send data, they do not know whether the other party has received the data or not, and whether the data is received 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 and use the function socket();   2. Set the socket properties and use the function setsockopt( );* Optional   3. Bind IP address, port and other information to the socket, use the function bind();* Optional   4. Set the IP address and port and other attributes 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、设置socket属性,用函数setsockopt();* 可选 
  3、绑定IP地址、端口等信息到socket上,用函数bind(); 
  4、循环接收数据,用函数recvfrom(); 
  5、关闭网络连接; 

UDP编程的客户端一般步骤是: 
  1、创建一个socket,用函数socket(); 
  2、设置socket属性,用函数setsockopt();* 可选 
  3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选 
  4、设置对方的IP地址和端口等属性; 
  5、发送数据,用函数sendto(); 
  6、关闭网络连接;

UDP补充:
   UDP不提供复杂的控制机制,利用IP提供面向无连接的通信服务。并且它是将应用程序发来的数据在收到的那一刻,立刻按照原样发送到网络上的一种机制。 即使是出现网络拥堵的情况下,UDP也无法进行流量控制等避免网络拥塞的行为。此外,传输途中如果出现了丢包,UDO也不负责重发。甚至当出现包的到达顺序乱掉时也没有纠正的功能。如果需要这些细节控制,那么不得不交给由采用UDO的应用程序去处理。换句话说,UDP将部分控制转移到应用程序去处理,自己却只提供作为传输层协议的最基本功能。UDP有点类似于用户说什么听什么的机制,但是需要用户充分考虑好上层协议类型并制作相应的应用程序。

TCP补充:
  TCP充分实现了数据传输时各种控制功能,可以进行丢包的重发控制,还可以对次序乱掉的分包进行顺序控制。而这些在UDP中都没有。此外,TCP作为一种面向有连接的协议,只有在确认通信对端存在时才会发送数据,从而可以控制通信流量的浪费。 TCP通过检验和、序列号、确认应答、重发控制、连接管理以及窗口控制等机制实现可靠性传输。

TCP与UDP区别总结:
1、TCP面向连接(如打电话要先拨号建立连接);UDP是无连接的,即发送数据之前不需要建立连接
2、TCP提供可靠的服务。也就是说,通过TCP连接传送的数据,无差错,不丢失,不重复,且按序到达;UDP尽最大努力交付,即不保   证可靠交付
3、TCP面向字节流,实际上是TCP把数据看成一连串无结构的字节流;UDP是面向报文的
  UDP没有拥塞控制,因此网络出现拥塞不会使源主机的发送速率降低(对实时应用很有用,如IP电话,实时视频会议等)
4、每一条TCP连接只能是点到点的;UDP支持一对一,一对多,多对一和多对多的交互通信
5、TCP首部开销20字节;UDP的首部开销小,只有8个字节
6、TCP的逻辑通信信道是全双工的可靠信道,UDP则是不可靠信道

TCP 与 UDP 的应用场景

  从特点上我们已经知道,TCP 是可靠的但传输速度慢 ,UDP 是不可靠的但传输速度快。因此在选用具体协议通信时,应该根据通信数据的要求而决定。
  若通信数据完整性需让位与通信实时性,则应该选用 TCP 协议(如文件传输、重要状态的更新等);反之,则使用 UDP 协议(如视频传输、实时通信等)。
使用UDP和TCP协议的各种应用和应用层协议:

TCP和UDP是OSI模型中的运输层中的协议。TCP提供可靠的通信传输,而UDP则常被用于让广播和细节控制交给应用的通信传输。

参考:https://blog.csdn.net/li_ning_/article/details/52117463
          http://network.51cto.com/art/201411/456783.htm

Guess you like

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