16 lines
446 B
Python
16 lines
446 B
Python
import socket
|
|
|
|
# 绑定 IP 和端口
|
|
UDP_IP = "0.0.0.0" # 监听所有网络接口
|
|
UDP_PORT = 9999
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
sock.bind((UDP_IP, UDP_PORT))
|
|
|
|
print(f"UDP 服务器已启动,监听端口 {UDP_PORT}...")
|
|
|
|
while True:
|
|
data, addr = sock.recvfrom(1024) # buffer size 1024 bytes
|
|
print(f"Received message from {addr}: {data.decode()}")
|
|
sock.sendto(b"Message received", addr) # send response
|