##基于UDP协议实现QQ对话,可以实现一对多,服务端一个,客户端可以有多个
基本实现了对话功能,但是也是需要你来我往交互,一方不能连续发送两条数据
##服务端程序
import socketserver
class MyUdphandler(socketserver.BaseRequestHandler):
def handle(self):
data, sock = self.request
print(data.decode('utf-8'))
info = input('=====>>').strip()
sock.sendto(info.encode('utf-8'), self.client_address)
if __name__ == '__main__':
server = socketserver.ThreadingUDPServer(('127.0.0.1', 8081), MyUdphandler)
server.serve_forever()
##客户端程序
from socket import *
client = socket(AF_INET, SOCK_DGRAM)
while True:
# client.sendto(b'hello', ('127.0.0.1', 8081))
info = input("=====>>>").strip()
client.sendto(info.encode('utf-8'),('127.0.0.1',8081))
data, server_addr = client.recvfrom(1024)
print(data.decode('utf-8'))