当前位置:网站首页>The difference between TCP and UDP
The difference between TCP and UDP
2022-06-11 06:49:00 【Dare you look at the avatar for three seconds?】
TCP And UDP The basic difference
Based on connection and no connection
TCP More system resources are required ,UDP Less ;
UDP The program structure is simple
Flow mode (TCP) And datagram mode (UDP);
TCP Make sure the data is correct ,UDP You may lose your bag
TCP Ensure the order of data ,UDP No guarantee
UDP Application scenarios
Datagram oriented
Most of the network data is short message
Have a lot of Client
No special requirements for data security
The burden of the Internet is very heavy , But it requires high response speed
Specific programming differences
socket() Different parameters
UDP Server No call required listen and accept
UDP Send and receive data sendto/recvfrom function
TCP: The address information is in connect/accept When determining
UDP: stay sendto/recvfrom Every time in the function Need to specify address information
UDP:shutdown Invalid function
Programming differences
Usually when we talk about network programming, the default is TCP Programming , That is to say, use the above-mentioned socket Function to create a socket be used for TCP Communications , We usually fill in the function parameters as SOCK_STREAM. namely socket(PF_INET, SOCK_STREAM, 0), This means building a socket For streaming network communication .
SOCK_STREAM This feature is connection oriented , That is, before sending and receiving data each time, you must pass connect Establishing a connection , It's also two-way , That is, either party can send and receive data , The agreement itself provides some guarantee mechanisms to ensure that it is reliable 、 Orderly , That is, each packet arrives at the receiver in the order of sending .
and SOCK_DGRAM This is a User Datagram Protocol Protocol network communication , It's connectionless , unreliable , Because after sending the data, the communication parties do not know whether the other party has received the data , Whether the data is received normally . Either party establishes a socket You can use it later sendto send data , It can also be used. recvfrom receive data . Don't care if the other person exists , Whether data has been sent . Its characteristic is that the communication speed is relatively fast . Everybody knows TCP It takes three handshakes , and UDP No, .
Based on the above differences ,UDP and TCP The programming steps are also somewhat different , as follows :
TCP:
TCP The general steps of programming on the server side are :
1、 Create a socket, Use functions socket();
2、 Set up socket attribute , Use functions setsockopt(); * Optional
3、 binding IP Address 、 Port and other information to socket On , Use functions bind();
4、 Turn on monitoring , Use functions listen();
5、 Receive connections from clients , Use functions accept();
6、 Sending and receiving data , Use functions send() and recv(), perhaps read() and write();
7、 Turn off network connection ;
8、 lsnrctl stop ;
TCP The general procedure of programming client is :
1、 Create a socket, Use functions socket();
2、 Set up socket attribute , Use functions setsockopt();* Optional
3、 binding IP Address 、 Port and other information to socket On , Use functions bind();* Optional
4、 Set the IP Properties such as address and port ;
5、 Connect to server , Use functions connect();
6、 Sending and receiving data , Use functions send() and recv(), perhaps read() and write();
7、 Turn off network connection ;
UDP
Corresponding UDP Programming is a lot easier , They are as follows :
UDP The general steps of programming on the server side are :
1、 Create a socket, Use functions socket();
2、 Set up socket attribute , Use functions setsockopt();* Optional
3、 binding IP Address 、 Port and other information to socket On , Use functions bind();
4、 Loop receiving data , Use functions recvfrom();
5、 Turn off network connection ;
UDP The general procedure of programming client is :
1、 Create a socket, Use functions socket();
2、 Set up socket attribute , Use functions setsockopt();* Optional
3、 binding IP Address 、 Port and other information to socket On , Use functions bind();* Optional
4、 Set up the other side's IP Properties such as address and port ;
5、 send data , Use functions sendto();
6、 Turn off network connection ;
TCP and UDP yes OSI Protocol in transport layer in model .TCP Provide reliable communication transmission , and UDP It is often used to allow broadcast and detail control to be handed over to the application for communication transmission .
UDP Add :
UDP No complex control mechanism , utilize IP Provide connectionless communication services . And it's sending data from the application at the moment it's received , A mechanism that immediately sends to the network as is . Even in the case of network congestion ,UDP Also can't carry on the flow control and so on to avoid the network congestion behavior . Besides , If there is a packet loss during transmission ,UDO I'm not responsible for resending . There is no correction function even when the arrival sequence of packets is disordered . If you need these details to control , Then I have to give it to you to adopt UDO The application to handle . let me put it another way ,UDP Transfer some control to the application to handle , They only provide the most basic functions as transport layer protocol .UDP It's a bit like the mechanism of what users say and listen to , But we need users to fully consider the type of the upper layer protocol and make corresponding applications .
TCP Add :
TCP It fully realizes various control functions during data transmission , Retransmission control of packet loss can be carried out , It can also control the order of the disordered subcontracting . And these are UDP None of them . Besides ,TCP As a connection oriented protocol , Data will be sent only when the communication peer is confirmed to exist , So we can control the waste of communication flow .TCP Pass the test and 、 Serial number 、 Confirm response 、 Resend control 、 Connection management and window control mechanism to achieve reliable transmission .
TCP And UDP Sum up the difference :
1、TCP Connection oriented ( If you want to make a call, dial to establish a connection );UDP It's disconnected , That is, you don't need to establish a connection before sending data
2、TCP Provide reliable service . in other words , adopt TCP Connect the transmitted data , No mistakes , No loss , No repetition , And arrive in order ;UDP Do your best to deliver , I.e. not guaranteed To certify reliable delivery
3、TCP Byte stream oriented , It's actually TCP Think of data as a stream of unstructured bytes ;UDP It's message oriented
UDP no congestion control , Therefore, the network congestion will not reduce the transmission rate of the source host ( Useful for real-time applications , Such as IP Telephone , Real time video conference, etc )
4、 Every one of them TCP Connections can only be point-to-point ;UDP Support one-to-one , One to many , Many to one and many to many interactive communication
5、TCP First cost 20 byte ;UDP The cost of the first part is small , Only 8 Bytes
6、TCP The logical communication channel of is a full duplex reliable channel ,UDP It's an unreliable channel
边栏推荐
- 无心剑汉英双语诗001.《爱》
- byte和bit的区别
- client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
- []==! []
- UEFI finding PCI devices
- Scripy web crawler series tutorials (I) | construction of scripy crawler framework development environment
- Handwritten promise [01] - Implementation of promise class core logic
- 2021-03-25
- 538. convert binary search tree to cumulative tree
- 微信小程序开发(原生和uniapp)DOM标签对比介绍
猜你喜欢

VTK-vtkPlane和vtkCutter使用

Do you use typescript or anyscript

无心剑汉英双语诗001.《爱》
![JS implementation of graphic merging and sorting process [source code attached]](/img/c8/210ddab791eb2319519496f7c7d010.jpg)
JS implementation of graphic merging and sorting process [source code attached]
![Illustration of JS implementation from insertion sort to binary insertion sort [with source code]](/img/e5/1956af15712ac3e89302d7dd73f403.jpg)
Illustration of JS implementation from insertion sort to binary insertion sort [with source code]
![Quick sorting of graphic array [with source code]](/img/ef/b1b98db5b16f0c4efc8d3c5247e8b0.jpg)
Quick sorting of graphic array [with source code]

Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。

网狐游戏服务器房间配置向导服务定制功能页实现

The nearest common ancestor of 235 binary search tree

Differences between FindIndex and indexof
随机推荐
微信小程序开发(原生和uniapp)DOM标签对比介绍
Notice on organizing the application for the first edition of Ningbo key software in 2022
Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
WPF 数据绑定(四)
563. slope of binary tree
Text overflow failure
538. convert binary search tree to cumulative tree
Flat design, blog website (VIII) code source code
Summary of string processing skills II
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
Count the time-consuming duration of an operation (function)
latex 各种箭头/带文字标号的箭头/可变长箭头
2021-03-25
Handwritten promise [01] - Implementation of promise class core logic
A highly controversial issue
无心剑汉英双语诗001.《爱》
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
UEFI查找PCI设备
Do you use typescript or anyscript
迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)