UDP
Tags: tutorial, linux, networking, python
Published: 2021-05-12 15:49:23

# UDP

### Introduction

Let's talk about this really funny protocol, funny is a weird word for a networking protocol, but I'll try to make my point. I will try the same approach as my last post on [TCP](https://dev.to/gealber/tcp-handshake-3ac), making a simple python script and taking a look to **Wireshark**. Let's start.

## Prerequisites

1. Python 3
2. Wireshark

## The UDP handshake, the what?

Yep in my opinion **UDP**(User Datagram Protocol) is barely a protocol, I know I'm a not a computer science guy, but still it doesn't seems to fit on that category of protocol. Let me clarify that **UDP is extremely useful**, I'm just saying that the parts involved on the communication doesn't follow too many rules, like in **TCP**. Instead of just saying this I will try to show you why I said this, let's make our simple Python script. Again this can be made with any programming language, I choose Python for simplicity.

Our `server.py` will contain our extremely simplified **UDP** server:

```bash
# UDP server.py

import socket

def main():
	# creating the socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # binding the socket to the address, in our case the localhost
    sock.bind(('localhost', 7456))
    print("Just here on 7456...")
    
    # start reading data,
    # I'm just interested on a single message from the client and it will be
    # pretty short so don't need more than this
    data, address = sock.recvfrom(1024)
    print(f"FROM: {address} DATA: {data}")

if __name__ == "__main__":
    main()

```

Note the difference here with **TCP**, in this case he just start listening without the need to call `socket.listen()` or `socket.accept()`, take a look at this question on [**Stackoverflow**](https://stackoverflow.com/questions/8194323/why-the-listen-function-call-is-not-needed-when-use-udp-socket#:~:text=UDP%20is%20a%20connectionless%2C%20unreliable,any%20order%20from%20any%20source.&text=It's%20because%20UDP%20is%20not,accept%20like%20there's%20for%20SOCK_STREAM), yes I use **Stackoverflow** as reference.

What about the client, here we go:

```bash
# UDP client.py

import socket

def main():
	# create the socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print("Sending data...")
    # and ready to go
    sock.sendto(b"Hello Lola", ('localhost', 7456))

if __name__ == "__main__":
    main()
```

We create a socket and ready to send message, take into account that you don't care if the server is listening, if there's a server, you just send and hope for the best.

### How this look like on Wireshark?

In the last post we saw that a single **TCP** connection with only one message generate several back and forward between the client and the server. I recommend you to take a look at the steps that you take to sniff the traffic in that case, in this case the only difference is that the filter in this case is `udp.port == 7456`.

![TCP on Wireshark](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jy338av8xbsrwfy6jidp.png)

Now let's look this but with **UDP**, this is going to be funny because well...let's see:

![UDP Wireshark](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0um2g4x0q7u9aqb84nrg.png)

Wait what? Just one message, the client just literally sent the message, he doesn't care if the server is listening because again there's not such a thing as listening on **UDP**.

![Balls on your face, UDP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvdka6p3rv5ip38s99kj.png)

Yep this picture explains really well the situation, also was made with [exalidraw](https://github.com/excalidraw/excalidraw), you know promoting open source projects :smiley:.

So **there's no handshake in UDP**, looking at the packet sent we can see that there's no too much to see:

![UDP packet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5h5glh6m37578ea43d3.png)

Only Destination port, Source port, UDP length and Checksum and good to go, sent that message!!

## Uses cases of UDP

You may be wondering, "So why do I need this?". Well given that **UDP** has low latency it has some pretty interesting uses cases. I'll let you these two links below, so you can explore them:

* [Stackoverflow question, UDP use cases](https://stackoverflow.com/questions/3751950/what-are-common-udp-usecases)
* [What is User Datagram Protocol](https://www.emnify.com/en/resources/udp)

That's all for today :wave:.

## Bibliography

1. [Stackoverflow](stackoverflow.com)