UDP Service Side & client programming
" "UDP programming creates a socket object, socket. Sock_dgram bind IP and Port,bind () method transfer data 1. Receive data, Socket.recvfrom (Bufsize[,flags]), get a 2-yuan ancestor (string,address) 2. Send data, SOCK Et.sendto (string,address), send to an address information release resources" "ImportSocketserver= Socket.socket (type=socket. SOCK_DGRAM) Server.bind (('0.0.0.0', 9999)) Data= SERVER.RECV (1024)#Blocking Wait Datadata = Server.recvfrom (1024)#Blocking wait data (value, (ip,port))Server.sendto (b'Hello',('127.0.0.1', 10000) ) Server.close ()" "the UDP client programming process creates the socket object, socket. Sock_dgram send data, socket.sendto (string,address) sent to an address information to receive data, Socket.recvfrom (Bufsize[,flags]), to get a 2-yuan ancestor (string, Address) to release resources" "Client= Socket.socket (type=socket. SOCK_DGRAM) raddr= ('127.0.0.1', 10000) Client.connect (raddr) client.sendto (b'Hello', raddr) data= CLIENT.RECV (1024)#Blocking Wait Datadata = Client.recvfrom (1024)#Blocking wait data, (value, (ip,port))Client.close ()
Note: There is no connection protocol on UDP, so there can be only one end, such as client data destined to the server, server presence or not important UDP socket object created, when no local address and Port bind () can specify the local address and Port LADDR, The immediate use of Connect () can immediately occupy the local address and port, fill the remote address and Port Raddrsendto () can immediately occupy the local address and port, and send the data to the specified remote, only with the local bound port, SendTo can send data to any far end () need to use with connect (), you can use the data from the local port to the RADDR specified remote recv () requirements must be occupied by the local port, Returns the received Data Recvfrom () requires that the local port must be occupied, returning a two-tuple of the received data and the peer address
The socket_udp of Python