服务端
# from socket import * # server=socket(AF_INET,SOCK_DGRAM) # server.bind(('127.7.8.9',7890)) # while True: # data,client_addr=server.recvfrom(1024) # server.sendto(data.upper(),client_addr) # from socket import * # server=socket(AF_INET,SOCK_DGRAM) # server.bind(('127.7.8.9',7891)) # # data,client_addr=server.recvfrom(1024) # print('第一次:',data) # # data,client_addr=server.recvfrom(1024) # print('第二次:',data) ''' 第一次: b'hello' 第二次: b'world' ''' from socket import * server=socket(AF_INET,SOCK_DGRAM) server.bind(('127.7.8.9',7893)) data,client_addr=server.recvfrom(3) print('第一次:',data) data,client_addr=server.recvfrom(3) print('第二次:',data) ''' OSError: [WinError 10040] 一个在数据报套接字上发送的消息大于内部消息缓冲区或其他一些网络限制,或该用户用于接收数据报的缓冲区比数据报小。 '''
客户端
# from socket import * # client=socket(AF_INET,SOCK_DGRAM) # while True: # msg=input('>>:').strip() # client.sendto(msg.encode('utf-8'),('127.7.8.9',7890)) # data,server_addr=client.recvfrom(1024) # print(data.decode('utf-8')) from socket import * client=socket(AF_INET,SOCK_DGRAM) client.sendto('hello'.encode('utf-8'),('127.7.8.9',7893)) client.sendto('world'.encode('utf-8'),('127.7.8.9',7893))