# udp_listen.py - lang nghe goi UDP tren port 9002. Moi goi la mot thong diep rieng, khong co bat tay.
import socket

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.bind(("127.0.0.1", 9002))        # doi thanh 0.0.0.0 de nhan tu may khac
    print("Dang nghe UDP o 127.0.0.1:9002 ...")
    while True:
        data, addr = s.recvfrom(2048)
        print(f"Tu {addr[0]}:{addr[1]} -> {data.decode('utf-8', errors='replace')}")
