Use a single long‑running socket (don’t call shutdown()
inside the loop), give it a timeout so recvfrom()
raises socket.timeout
instead of blocking forever, and be sure you’re checking for replies coming from the device’s port (5000), not your bind port (6000). For example: bind to 0.0.0.0:6000, call sock.settimeout(1.0)
, send your byte, then wrap recvfrom()
in a try/except to catch timeouts and retry, only responding when you get 0xFF
from 192.168.0.2:5000, and finally call sock.close()
when you actually want to exit. That way your loop keeps running without stalls and only closes when you’re done.