Learn more about Russian war crimes in Ukraine.

What syscalls does a UDP server need?

I previously described the syscalls needed for a multi-client TCP server. There are at least eight: socket, bind, listen, select, accept, recv, send, and close. This could be used to make an echo server: clients can open a TCP connection, and whatever bytes they send get echoed back.

Now let’s look at the simplest UDP server, and again make an echo server: clients can send a UDP datagram, and whatever they send gets echoed back.

  1. udp_fd = socket(UDP): “OS, please create a UDP socket, and give me a file descriptor referencing it”.
  2. bind(udp_fd, 9999): “OS, please point UDP port 9999 to the new UDP socket”.
  3. msg, from = recvfrom(udp_fd): “OS, please put me to sleep until a UDP datagram is received, then give it to me”.
  4. sendto(udp_fd, msg, from): “OS, please send this UDP datagram from my port to this remote address and port”.
  5. close(udp_fd): “OS, I’m done listening for new datagrams. Stop listening on that port, and remove this file descriptor”.

Compared to the TCP server, we of course don’t get the features of TCP: messages are unreliable, unordered, and nothing monitors network congestion.

But a benefit is simplicity: we have no listen, select, or accept. We do not have to manage multiple connections, deal with splitting byte streams into individual messages, or work with select/kqueue/epoll.

The other benefit is flexibility: our communication may not fit into the TCP model very well.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2016. This post is not associated with my employer. Found an error? Edit this page.