UDP Sockets: Fast Connectionless Datagrams
UDP (`socket.SOCK_DGRAM`) is connectionless. Packets are fired directly to target addresses with `.sendto()` without handshakes, trading reliability for raw speed 🚀.
import socket
# Create UDP socket:
udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Transmit datagram directly to destination without .connect():
# udp_sock.sendto(b"ping", ("127.0.0.1", 9999))
print(f"UDP Socket initialized: {udp_sock.type == socket.SOCK_DGRAM} 🚀")
udp_sock.close()
# 73. UDP Sockets: Fast Connectionless Datagrams UDP (`socket.SOCK_DGRAM`) is connectionless. Packets are fired directly to target addresses with `.sendto()` without handshakes, trading reliability for raw speed 🚀. ```python import socket # Create UDP socket: udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Transmit datagram directly to destination without .connect(): # udp_sock.sendto(b"ping", ("127.0.0.1", 9999)) print(f"UDP Socket initialized: {udp_sock.type == socket.SOCK_DGRAM} 🚀") udp_sock.close() ``` UDP has no delivery confirmation or packet reordering. It powers DNS lookups, video calls, and multiplayer games where speed trumps dropped packets! 🚀 --- ### Question Which socket constant creates a connectionless UDP socket? 🧠- [ ] a. `socket.SOCK_DGRAM` 🚀 - [ ] b. `socket.SOCK_STREAM` - [ ] c. `socket.SOCK_RAW` - [ ] d. `socket.SOCK_SEQPACKET` --- ### Challenge Import `socket`. Create a UDP socket `u = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)`. Print `u.type == socket.SOCK_DGRAM`. Close `u`. 🎯 <details> <summary>Solution</summary> ```python import socket u = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) print(u.type == socket.SOCK_DGRAM) u.close() ``` </details>