Creating a simple ChatApp using User Datagram Protocol(UDP) in Python

Arjun Chauhan
4 min readJan 7, 2021

--

Whenever we start learning about networking we often stumble upon two protocols:

  1. Transmission Control Protocol (TCP)
  2. User Datagram Protocol (UDP)

So how are these two protocols different?

UDP

User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a part of Internet Protocol suite, referred as UDP/IP suite. Unlike TCP, it is unreliable and connection less protocol. So, there is no need to establish connection prior to data transfer. Although it is insecure it is very fast and provides very low latency.

TCP

TCP is used for organizing data in a way that ensures the secure transmission between the server and client. It guarantees the integrity of data sent over the network, regardless of the amount. For this reason, it is used to transmit data from other higher-level protocols that require all transmitted data to arrive.

It is a connection oriented protocol. In layman terms when a user sends a request packet using TCP protocol the connection is not ended once the package is sent. In contrary the receiver sends a packet back to the sender.

TCP vs UDP

TCP is a connection oriented protocol. UDP is a connection less protocol. As TCP provides error checking support and also guarantees delivery of data to the destination router this make it more reliable as compared to UDP. … On other hand retransmission of packets is not possible in UDP.

In today’s blog I would be creating a small chat application. Using it you can easily connect between two systems within a network it allows to send messages through UDP protocol.

Let’s view the connections

For the practical I would be using two systems. One is Windows 10 and the other is RHEL8.

This code is written in my windows 10 OS. I would be explaining everything from the windows perspective only because on the RHEL8 system we have exact same configuration.

This code shows two functions and the task they do is quite self explanatory by the function name.

The UDP protocol does not maintain the connection. So when chatting each machine will act both as a sender and receiver.

myconnect: is responsible for creating a socket which is configured to handle ipV4 connections which is specified by <socket.AF_INET> at the same time <socket.SOCK_DGRAM> tells it that the protocol that would be used to be UDP.

Then we have to specify the IP of windows system and the port from which it would be listening . After that we bind it to myconnect. Hence myconnect can be considered as a working socket that listens on port 2323 for someone to send message. This function is hence responsible for receiving the message.

We also want it to send message. For this it needs to connect to the server which is running on the different VM.

For this we again have to create the socket but this type we have to specify the IP of the destination and the port to which it needs to connect. This port would be the one in which the server of the other machine would be listening to.

A similar configuration is done on the RHEL8 system too.

So I created a while loop so that the code does not terminate once the message is shared between the systems. First we take the input from the user and encode it to bytes. This is because that the data can be transferred between the networks in form of bytes only. Then we send it to the RHEL8 OS. After the message is sent we get ready to receive the message from the RHEL8 OS. Once it is done the cycle repeats again.

This is the code from the RHEL8 it is similar to the one configured in the windows system.

The only difference is that it receives the message first and sends later.

To see the app in action watch this video I made.

Conclusion

The similar task can be performed using TCP protocol in a much easier way but the point is not of doing things easy. I configured it using UDP because it presents more challenging task and helps to get the concept cleared in a much more effective way.

--

--