1.UDP Service-Side server
1 fromSocketImport*2 fromTimeImportCTime3 4HOST ="'5PORT = 88886Bufsiz = 10247ADDR =(HOST, PORT)8 9Udpsersock =socket (af_inet, SOCK_DGRAM)Ten Udpsersock.bind (ADDR) One A whileTrue: - Print('waiting for message ...') -data, addr =Udpsersock.recvfrom (Bufsiz) the -Content ='[%s]%s'% (bytes (CTime (),"Utf-8"), data) -Udpsersock.sendto (Content.encode ("Utf-8"), addr) - Print('... received from and returned to:', addr) + -Udpsersock.close ()
2.UDP Client Clients
1 fromSocketImport*2 3HOST ='localhost'4PORT = 88885Bufsiz = 10246ADDR =(HOST, PORT)7 8Udpclisock =socket (af_inet, SOCK_DGRAM)9 Ten whileTrue: Onedata = input ('>') A if notData: - Break - theUdpclisock.sendto (Data.encode ("Utf-8"), ADDR) -data, ADDR =Udpclisock.recvfrom (Bufsiz) - if notData: - Break + Print(data) - +Udpclisock.close ()
Python creates UDP server and client