Lesson 6 of 15
UDP: User Datagram Protocol
UDP: User Datagram Protocol
UDP is a connectionless, lightweight transport protocol. Unlike TCP, it does not guarantee delivery, ordering, or duplicate protection — but it is significantly faster.
UDP vs TCP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | Best-effort |
| Ordering | Ordered | Unordered |
| Overhead | High (headers, handshake) | Low (8-byte header) |
| Use cases | Web, email, file transfer | Gaming, DNS, video streaming |
UDP Datagram Header
0 15 16 31
+--------+--------+
| Src Port|Dst Port|
+--------+--------+
| Length |Checksum|
+--------+--------+
| Data |
+-----------------+
The header is only 8 bytes: source port (2), destination port (2), length (2), and checksum (2).
Checksum
UDP uses a simple checksum to detect corruption. A basic checksum sums all bytes in the data and takes the result modulo 256.
Your Task
Implement createUDPDatagram(srcPort, dstPort, data) that creates a simulated UDP datagram object with:
srcPort,dstPort,length(header 8 bytes + data length),checksum(sum of char codes mod 256),data
Also implement verifyChecksum(datagram) that returns true if the checksum is correct.
Node.js loading...
Loading...
Click "Run" to execute your code.