Lesson 22: UDP Datagrams — sendto and recvfrom
Unlike TCP, UDP (User Datagram Protocol) does not establish a connection, does not guarantee order, and does not guarantee delivery. That sounds like a disadvantage — but precisely because of this UDP is faster and simpler, suitable for scenarios where you can afford to lose a single packet but cann
UDP is like mailing a postcard. You write the address, drop it in the mailbox — done. No confirmation it arrived, no knowledge of ordering with other postcards. But it is cheap, fast, and direct. TCP by contrast is a tracked package with signature confirmation and insurance. For GPU metrics sent every second, a postcard is perfectly sufficient.
- UDP
- A connectionless transport protocol. Sends datagrams without a connection, with no guarantee of order or delivery. Faster and simpler than TCP.
- sendto()
- Sends a UDP datagram to a destination address specified in each call. Unlike send(), no prior connection is required.
- recvfrom()
- Receives a UDP datagram and returns both the data and the sender's address. Allows sending a reply back to the specific sender.
- datagram
- An independent unit of data in UDP. Each datagram contains source address, destination address, and data. It is independent of previous or future datagrams.
- connectionless
- A communication model where each message is independent and there is no connection setup process (handshake). UDP is connectionless; TCP is connection-oriented.