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.
Server
#-*-Coding:utf8-*-Importsocket, ThreadingdefUdplink (S, data, addr):Print 'Received from%s:%s'%addr, Data s.sendto ('Hello,%s'%data, addr)#sock_dgram Specifies that the type of socket is UDPs =Socket.socket (socket.af_inet, socket. SOCK_DGRAM)#Binding PortS.bind (('127.0.0.1', 9999))#UDP programming does not require monitoringPrint 'Bind UDP on 9999 ...' whileTrue:data, addr= S.recvfrom (1024) T= Threading. Thread (Target=udplink, args=(S, data, addr)) T.start ()
Client
#-*-Coding:utf8-*-Importsocket, times=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) forDatainch['David','Yt','Amenda']: #Send data:S.sendto (Data, ('127.0.0.1', 9999)) #Receive data: PrintS.RECV (1024) Time.sleep (2) S.close ()
2015-05-11
Python's UDP programming