Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Basic UDP Communication | UDP and HTTP Communication
Python Networking Basics

bookBasic UDP Communication

When you need fast, connectionless communication between computers, User Datagram Protocol (UDP) is often the protocol of choice. UDP is widely used for applications where speed is more important than reliability, such as real-time video streaming, online gaming, and simple service discovery on local networks. Unlike TCP, UDP does not establish a persistent connection or guarantee delivery, ordering, or error correction of packets. This makes UDP lightweight and efficient, but also means you must handle any lost or out-of-order data yourself if your application requires it.

1234567891011121314151617181920212223
# UDP Sender import socket udp_ip = "127.0.0.1" # Localhost udp_port = 5005 message = b"Hello, UDP!" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(message, (udp_ip, udp_port)) print(f"Sent message: {message.decode()} to {udp_ip}:{udp_port}") # UDP Receiver import socket udp_ip = "127.0.0.1" udp_port = 5005 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((udp_ip, udp_port)) print(f"Listening for UDP packets on {udp_ip}:{udp_port}...") data, addr = sock.recvfrom(1024) # Buffer size is 1024 bytes print(f"Received message: {data.decode()} from {addr}")
copy

In the code above, you see two roles: a UDP sender and a receiver, both using Python's socket module. The sender creates a UDP socket and sends a message to a specific IP and port. The receiver binds to the same IP and port, then waits to receive a datagram. The key difference between UDP and TCP here is that UDP does not require you to call listen() or accept() on the server side, and there is no handshake or persistent connection. UDP simply sends discrete packets, which may arrive out of order, be duplicated, or not arrive at all. Because of this, you should be careful when using UDP for applications where data integrity is important, and always test for packet loss or duplication if reliability matters to your use case.

question mark

Which statement best describes a key characteristic of UDP communication in Python?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookBasic UDP Communication

Swipe um das Menü anzuzeigen

When you need fast, connectionless communication between computers, User Datagram Protocol (UDP) is often the protocol of choice. UDP is widely used for applications where speed is more important than reliability, such as real-time video streaming, online gaming, and simple service discovery on local networks. Unlike TCP, UDP does not establish a persistent connection or guarantee delivery, ordering, or error correction of packets. This makes UDP lightweight and efficient, but also means you must handle any lost or out-of-order data yourself if your application requires it.

1234567891011121314151617181920212223
# UDP Sender import socket udp_ip = "127.0.0.1" # Localhost udp_port = 5005 message = b"Hello, UDP!" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(message, (udp_ip, udp_port)) print(f"Sent message: {message.decode()} to {udp_ip}:{udp_port}") # UDP Receiver import socket udp_ip = "127.0.0.1" udp_port = 5005 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((udp_ip, udp_port)) print(f"Listening for UDP packets on {udp_ip}:{udp_port}...") data, addr = sock.recvfrom(1024) # Buffer size is 1024 bytes print(f"Received message: {data.decode()} from {addr}")
copy

In the code above, you see two roles: a UDP sender and a receiver, both using Python's socket module. The sender creates a UDP socket and sends a message to a specific IP and port. The receiver binds to the same IP and port, then waits to receive a datagram. The key difference between UDP and TCP here is that UDP does not require you to call listen() or accept() on the server side, and there is no handshake or persistent connection. UDP simply sends discrete packets, which may arrive out of order, be duplicated, or not arrive at all. Because of this, you should be careful when using UDP for applications where data integrity is important, and always test for packet loss or duplication if reliability matters to your use case.

question mark

Which statement best describes a key characteristic of UDP communication in Python?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
some-alt