udp
服务端
引用socket
=类型,协议
绑定地址 和 端口
while 循环
收到 data addr = 服务。recvfrom(1024)
发送 服务。sendto(data,addr(ip 端口))
客户端
引用socket
=类型,协议
while 循环
发送 msg =input().sticp
kehu.sendto(msg.encode('utf-8'),addr(ip 和端口))
收到 data,fuwu.addr = kehu.recvfrom(1024)
print(data.decode('utf-8))
服务端的:
from socket import *
server = socket(AF_INET,SOCK_DGRAM)
server.bind(('127.0.0.1',8800))
while True:
data,client_addr = server.recvfrom(1024)
print('客户端的数据:',data)
server.sendto(data.upper(),client_addr)
客户端:
from socket import *
client = socket(AF_INET,SOCK_DGRAM)
while True:
msg = input('>>:').strip()
client.sendto(msg.encode('utf-8'),('127.0.0.1',8800)) 发送
data ,server = client.recvfrom(1024)
print(data.decode('utf-8'))