UDP questions

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

is it possible to send a packet to UDP port 90,000?


the port field in a TCP or UDP packet is 16 bits. 2^16 is 65536, so the maximum port number is 65535.

does every UDP packet have a destination port?


The UDP header is 8 bytes. Here's how it's laid out. According to the RFC, the source port is optional but the destination port is required..

 <-   16 bits  ->
|   source port  |    dest port   |
|     length     |    checksum    |

can you send a 1,000,000-byte UDP packet if you want?


the length field on a UDP packet is 16 bits, so the maximum length is 65535

can you put JSON in a UDP packet?


you can put any bytes you want in a UDP packet. You could even put a very short mp3 file!

are UDP packets guaranteed to arrive in the same order they were sent?


if you send a UDP packet, is there any way to tell if it arrived or not?

not built into the protocol, no!

if you want that, you'll need to implement that yourself

are UDP packets guaranteed to arrive if I send them to another server in the same datacenter?


even if they're sent inside the same computer, packets can still get dropped (for example if a buffer gets full)

when you send a UDP packet, what happens if it gets lost?

too bad! it's lost!

If you want to implement retries on top of UDP, you have to do it yourself. TCP packets do get retried by your operating system's TCP implementation.

is UDP port 80 the same as TCP port 80?


UDP & TCP both support the same port numbers (1-65535) but they're different protocols. You can run 2 different servers on UDP port 80 and TCP port 80 at the same time.

name one protocol that's built on top of UDP

DNS is a big one!
Some other examples: DHCP, QUIC, NTP, statsd, a lot of videoconferencing protocols and lots more.