基于UDP的socket套接字编程
- UDP协议是无连接的,所以不会有粘包问题
服务端
from socket import *
server = socket(AF_INET, SOCK_DGRAM)
server.bind(('127.0.0.1', 8000))
print('start...')
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:
cmd = input('please enter msg>>>')
client.sendto(cmd.encode('utf8'), ('127.0.0.1', 8000))
data = client.recvfrom(1024)
print('来自服务端数据:', data)