#!/usr/bin/env python
#-*-coding:utf-8-*-
#udp socketserver客户端
import socketHOST='127.0.0.1'
PORT=3214
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)data='你好!'
s.sendto(data.encode('utf-8'),(HOST,PORT))while data!='bye':dat=b''while len(dat)==0:dat,addr=s.recvfrom(1024)print('收到数据:',dat.decode('utf-8'))data=input('请输入要发送的信息: ')if data=='':data='bye's.sendto(data.encode('utf-8'),(HOST,PORT))s.close()#!/usr/bin/env python
#-*-coding:utf-8-*-
#udp 服务端链接
import socketserverclass MyHdl(socketserver.DatagramRequestHandler):def handle(self):data,socket=self.requestprint('收到: ',data.decode('utf-8'))socket.sendto(data,self.client_address)if __name__=='__main__':HOST=''PORT=3214 s=socketserver.UDPServer((HOST,PORT),MyHdl)s.serve_forever()#!/usr/bin/env python
#-*-coding:utf-8-*-
#udp 客户端
import socket
HOST='127.0.0.1'
PORT=3214
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)data="你好!"while data:s.sendto(data.encode('utf-8'),(HOST,PORT))if data=='bye':breakdata,addr=s.recvfrom(1024)print('Receive from Server:\n',data.decode('utf-8'))data=input('Please input an info:\n')s.close()
#!/usr/bin/env python
#-*-coding:utf-8-*-
#udp 服务器端通信
import socket
HOST=''
PORT=3214
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind((HOST,PORT))
data=Truewhile data:data,addr=s.recvfrom(1024)if data==b'bye':breakprint('Recieve String:',data.decode('utf-8'))s.sendto(data,addr)s.close()