Server-side Pseudo-code:
SS = socket () Ss.bind () Inf_loop: = Ss.recvfrom ()/ ss.sendto () ss.close ()
Server side:
#Coding=utf-8 fromSocketImport*ImportTimehost='localhost'PORT= 12345Bufsiz= 1024ADDR=(HOST, PORT) Udpsersock= Socket (type=sock_dgram) Udpsersock.bind (ADDR) whileTrue:Print('waiting for message ...') data, addr=Udpsersock.recvfrom (bufsiz) udpsersock.sendto (bytes (Time.ctime (),'Utf-8'), addr)Print('received from and returned to', addr) udpsersock.close ()
Client side:
Pseudo code:
CS = socket () Comm_loop: = cs.sendto/ recvfrom () cs.close ()
#Coding=utf-8 fromSocketImport*HOST='localhost'PORT= 12345Bufsiz= 1024ADDR=(HOST, PORT) Udpclisock= Socket (type=sock_dgram) whileTrue:data= Input ('>') if notData: Breakudpclisock.sendto (bytes (data,'Utf-8'), ADDR) data, ADDR=Udpclisock.recvfrom (Bufsiz)if notData: Break Print(data) udpclisock.close ()
Operation Result:
Python UDP timestamp server