Excerpt: Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001432004977916a212e2168e21449981ad65cd16e71201000
TCP is to establish a reliable connection , and both sides of the communication can send data in the form of a stream . In contrast to TCP,UDP is a non-connection-oriented protocol .
When using the UDP protocol, you do not need to establish a connection, just need to know the other side's IP address and port number , you can directly send packets. But I don't know if I can get there.
Although the transmission of data with UDP is unreliable, its advantage is that it is faster than TCP, and the UDP protocol can be used for data that does not require reliable arrival .
Let's take a look at how the data is transmitted over the UDP protocol. Like TCP, both sides of the communication using UDP are also divided into clients and servers. The server first needs to bind the port :
s = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)# bound port:s.bind (('127.0.0.1', 9999))
When a socket is created, the SOCK_DGRAM
type of the socket specified is UDP. The binding port is the same as TCP, but does not require a listen()
method to be called , but instead receives data from any client directly:
Python Learning Notes (46) network programming (2)-UDP programming