UDP入门:
服务端
import socket
BUF_SIZE = 1024
server_addr = ('127.0.0.1',8888)
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind(server_addr)
while True:
print ("waiting for data")
data,client_addr = server.recvfrom(BUF_SIZE)
print ('Connected by ',client_addr,' Receive data : ',data.decode('utf-8'))
server.sendto(data,client_addr)
server.close()
客户端
import socket
import struct
BUF_SIZE = 1024
server_addr = ('127.0.0.1',8888)
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
data =input("Please input some string > ")
client.sendto(data.encode('utf-8'),server_addr)
data,addr = client.recvfrom(BUF_SIZE)
print ("Data : ",data.decode('utf-8'))
client.close()