Serie: sockets
python
46 líneas
· Actualizado 2026-02-03
04-basic-udp-client.py
sockets/examples/python/04-basic-udp-client.py
#!/usr/bin/env python3
"""
Basic UDP Client
A simple UDP client that sends a message to a server.
Note: UDP is connectionless, so no connect() call needed.
"""
import socket
import sys
# Configuration
HOST = 'localhost'
PORT = 8080
def main():
# Create UDP socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Send message (sendto requires server address)
message = "Hello, UDP Server!"
print(f"Sending to {HOST}:{PORT}: {message}")
client.sendto(message.encode('utf-8'), (HOST, PORT))
# Receive response (recvfrom returns data and server address)
data, server_address = client.recvfrom(1024)
if data:
response = data.decode('utf-8')
print(f"Received from {server_address}: {response}")
else:
print("No response received")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
finally:
# Close socket
client.close()
print("Socket closed")
if __name__ == '__main__':
main()
Artículos relacionados
sockets
c
Actualizado 2026-02-05
01-basic-tcp-server.c
01-basic-tcp-server.c — c source code from the sockets learning materials (sockets/examples/c/01-basic-tcp-server.c).
Leer artículo →
sockets
c
Actualizado 2026-02-05
02-basic-tcp-client.c
02-basic-tcp-client.c — c source code from the sockets learning materials (sockets/examples/c/02-basic-tcp-client.c).
Leer artículo →
sockets
c
Actualizado 2026-02-05
03-basic-udp-server.c
03-basic-udp-server.c — c source code from the sockets learning materials (sockets/examples/c/03-basic-udp-server.c).
Leer artículo →
sockets
c
Actualizado 2026-02-05
04-basic-udp-client.c
04-basic-udp-client.c — c source code from the sockets learning materials (sockets/examples/c/04-basic-udp-client.c).
Leer artículo →
sockets
c
Actualizado 2026-02-05
05-https-client.c
05-https-client.c — c source code from the sockets learning materials (sockets/examples/c/05-https-client.c).
Leer artículo →
sockets
c
Actualizado 2026-02-05
06-domain-to-ip.c
06-domain-to-ip.c — c source code from the sockets learning materials (sockets/examples/c/06-domain-to-ip.c).
Leer artículo →