UDP

TCP and UDP (3 Part Series)

1 TCP handshake
2 UDP
3 MQTT over TCP

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, 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:

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

import socket

def main<span>()</span>:
    <span># creating the socket</span>
    sock <span>=</span> socket.socket<span>(</span>socket.AF_INET, socket.SOCK_DGRAM<span>)</span>
    <span># binding the socket to the address, in our case the localhost</span>
    sock.bind<span>((</span><span>'localhost'</span>, 7456<span>))</span>
    print<span>(</span><span>"Just here on 7456..."</span><span>)</span>

    <span># start reading data,</span>
    <span># I'm just interested on a single message from the client and it will be</span>
    <span># pretty short so don't need more than this</span>
    data, address <span>=</span> sock.recvfrom<span>(</span>1024<span>)</span>
    print<span>(</span>f<span>"FROM: {address} DATA: {data}"</span><span>)</span>

<span>if </span>__name__ <span>==</span> <span>"__main__"</span>:
    main<span>()</span>
# 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()

Enter fullscreen mode Exit fullscreen mode

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, yes I use Stackoverflow as reference.

What about the client, here we go:

<span># UDP client.py</span>
import socket
def main<span>()</span>:
<span># create the socket</span>
sock <span>=</span> socket.socket<span>(</span>socket.AF_INET, socket.SOCK_DGRAM<span>)</span>
print<span>(</span><span>"Sending data..."</span><span>)</span>
<span># and ready to go</span>
sock.sendto<span>(</span>b<span>"Hello Lola"</span>, <span>(</span><span>'localhost'</span>, 7456<span>))</span>
<span>if </span>__name__ <span>==</span> <span>"__main__"</span>:
main<span>()</span>
<span># UDP client.py</span>

import socket

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

<span>if </span>__name__ <span>==</span> <span>"__main__"</span>:
    main<span>()</span>
# 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()

Enter fullscreen mode Exit fullscreen mode

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.

Now let’s look this but with UDP, this is going to be funny because well…let’s see:

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.

Yep this picture explains really well the situation, also was made with exalidraw, you know promoting open source projects .

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

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:

That’s all for today .

Bibliography

  1. Stackoverflow

TCP and UDP (3 Part Series)

1 TCP handshake
2 UDP
3 MQTT over TCP

原文链接:UDP

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
Only they who fulfill their duties in everyday matters will fulfill them on great occasions.
只有在日常生活中尽责的人才会在重大时刻尽责
评论 抢沙发

请登录后发表评论

    暂无评论内容