Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Create a UDP socket, send an encoded message with sendto(), then wait for a reply with recvfrom(). The example below adds a two-second timeout so it does not wait indefinitely if no response arrives.
Send a UDP message and receive a reply
This IPv4 example sends the text hello to a service listening at 127.0.0.1:9999. Change the host, port and message to match the server and its protocol.
import socket
HOST = "127.0.0.1"
PORT = 9999
MESSAGE = "hello"
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.settimeout(2.0)
sock.sendto(MESSAGE.encode("utf-8"), (HOST, PORT))
try:
data, server_address = sock.recvfrom(4096)
except TimeoutError:
print("No response before timeout")
else:
print(
"Received",
data.decode("utf-8", errors="replace"),
"from",
server_address,
)
The server must be listening at the destination and understand the message format. Python’s UDP socket example shows the corresponding server-side receive-and-reply pattern.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →What each socket call does
socket.socket(socket.AF_INET, socket.SOCK_DGRAM)creates an IPv4 datagram socket. For IPv6, useAF_INET6and the address form required for that family.MESSAGE.encode("utf-8")converts Python text to bytes. The sending and receiving programs need to agree on the encoding and protocol.sendto(payload, (host, port))sends one datagram to the destination. A successful call means the local socket accepted the send; it does not confirm the remote application received or processed it.recvfrom(4096)waits for a datagram and returns its payload and sender address. Python documents the result as a pair of received bytes and the sending socket’s address in its socket reference.
The example decodes the response as UTF-8 only because it assumes the server uses that encoding. If the service returns binary data or another format, parse it according to that protocol instead. The buffer size is the maximum number of bytes this call accepts; a truncated response cannot be reconstructed from the returned data.
#1 Best Overall
Set a timeout and interpret the result
Socket operations block by default. Calling settimeout(2.0) makes a blocking operation raise TimeoutError if it does not complete within two seconds. Here that bounds the wait for a reply. It does not establish that the server is down: the request may have been lost, the server may not reply, or the reply may have been lost. Python documents timeout behavior and other socket errors in the socket timeout reference.
Other socket or address problems can raise OSError or a subclass. For a small script, letting unexpected errors surface can be useful; a long-running application should handle the specific errors it can recover from and log or report the rest.
Rank #2
Choose the right address and socket behavior
IPv4, IPv6 and hostnames
The example uses IPv4 and a numeric loopback address, so it targets the same machine. Replace it with the server’s reachable address. With a hostname, name resolution and host configuration can produce different addresses or families; use a numeric address when you need deterministic selection. For IPv6, create an AF_INET6 socket and provide an address tuple in the form appropriate to that family.
Blocking, timeout and non-blocking modes
A finite timeout is a straightforward choice for a request-and-reply script. You can use sock.settimeout(None) for the default blocking behavior, or sock.setblocking(False) when building an event-driven program that checks readiness separately. Non-blocking mode requires additional handling when no data is immediately available; it is not a drop-in way to avoid designing the wait behavior.
Fire-and-forget or request-and-response
If the client does not need a reply, omit recvfrom(). If it does, the application protocol should define how long to wait, whether to retry, how to identify requests, and how to handle duplicate replies. UDP itself does not supply acknowledgements, ordering or duplicate protection. RFC 768 describes UDP as transaction-oriented and says delivery and duplicate protection are not guaranteed (RFC 768).
Why a UDP client may not receive a response
- No server is listening: Verify the server is running and bound to the address and port the client targets.
- The server expects a different message: Check its required encoding, payload structure and request format. Sending text bytes is not enough if the service expects a binary or structured protocol.
- The network does not pass the datagram or reply: Firewalls, routing, NAT or packet loss can prevent either direction from working. UDP does not guarantee delivery.
- The response took longer than the timeout: Increase the wait only if the application permits it; a timeout reports that the local wait ended without a reply, not why.
- The endpoint selection differs from expectations: If using a hostname, check which address and family the system resolves and whether the server is reachable at that endpoint.
For diagnosis, confirm the server’s bind address and port, then verify that client and server agree on the message format and address family. A successful local send alone cannot distinguish a server problem from a lost request or reply.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Keep datagrams suitable for the path
UDP preserves datagram boundaries, but large messages may require IP fragmentation. Fragmentation can reduce reliability and efficiency, and a datagram size that works on one network path may fail on another. RFC 8085 recommends avoiding fragmentation where possible and discusses UDP application design and path constraints (RFC 8085). Prefer small application messages or split larger data into protocol-defined pieces with appropriate identification and recovery behavior.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteA zero-length UDP payload is valid; it is not an end-of-stream signal as it can be for a TCP stream. Applications must define what an empty datagram means.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

