Python 2

Create Simple Chat App Using UDP Protocol In Python

In this blog, we are going to create a chat server based on UDP protocol in Python.

Here’s the problem statement:

🔅 Create your own Chat Servers, and establish a network to transfer data using Socket Programing by creating both Server and Client machines as Sender and Receiver both. Do this program using UDP data transfer protocol.

🔅 Use multi-threading concept to get and receive data parallelly from both the Server Sides. Observe the challenges that you face to achieve this using UDP.

Before jump into code, let’s understand

What is UDP?

In computer networking, the User Datagram Protocol is one of the core members of the Internet protocol suite. With UDP, computer applications can send messages, in this case, referred to as datagrams, to other hosts on an Internet Protocol network.

It is a connectionless protocol and not reliable. It is not used in other chatting apps like Facebook and WhatsApp. The communication speed is comparatively fast than the TCP protocol. It is used in online video surfing and gaming.

Creating Chat server[One sided connection]

In this setup, Server only receives messages and the client can only send messages. We can utilize the socket library in python.

In server, we have to write the below code, which will continue in receiving mode using while true.

In client program:

we can also see the working of it using the below image

You can see on the client end we really don’t need to establish a connection with the client. We can directly send data to the server.

But the problem here is the server can not respond back and the client can not receive any message at the same time.

Here’s we can take help of multi-threading, by which we can receive as well send messages simultaneously.

Both sides communicate using multithreading

In python, we have a threading module, with help we can add many threads to run our program. we can also pass different functions to different threads to run.

Let’s understand with one help. consider the human body is a process and our parts are different threads of it. they are performing their tasks simultaneously. The liver, heart, lungs, brain are the organs involved in the process and each process is running simultaneously.


Here we are leveraging the power of threads to simultaneously send and receive the messages.

In the above code, I have two functions send() and rec() and we are starting them at the same time. The threading module gives us a Thread class and we can use that to make and start at the same time.

Type quit exiting the program.

UDP is not reliable protocol which does not check that other system is running or not because it is connectionless and does not have any acknowledgment.


We have successfully created a simple chat application using UDP protocol in python.

I would love to hear your thoughts and ideas about these topics. Don’t hesitate to share in the comment section below.

Also, you can always message me over LinkedIn as well.

More Ideas:

You can enhance this setup to send OS commands and get the output back, instead of logging in systems.

DevOps Engineer at Mindbowser Inc|