Python Chat App using Socket Programming via UDP Protocol & Multi-Threading!!

Sumayya Khatoon
4 min readJul 6, 2021

Hello Folks! Back with another Blog which will guide you to build a simple Chat Application using Socket Programming in Python. So, Guys let’s get going…

The Chat Application is very common today offered either via a web application or mobile application. A chat room is an interface that allows two or more people to chat and send messages to everyone in the room. Chat Application provides communication between two parties i.e. sender and receiver. The sender is someone who initiates and send a message to other known as receiver; receiver at other end receives the message. In networking terms, sender and receiver are denoted as source and destination respectively.

What is a Socket?

A socket, in networking terminologies, serves as an intermediate connecting the application layer to the transport layer in the TCP/IP protocol suite. These network sockets are present on the client-side and the server-side. In network communication, users are identified by a socket which is nothing but a combination of IP address and port address.

What is Socket Programming?

Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented(TCP) and connectionless(UDP) protocols.

Note: To create a Python Chat Application, one has to write a server program and client program (sender and receiver)😅. Here I’m gonna implement the practical with UDP Protocol. Also I’m including the concepts of Multi-threading to get and receive the data parallelly from both the Server and Client Sides.

Is there any Pre-requisites?

  • Umm🤔Yes, You need Two Operating System to implement this Practical. In my case, both the OS is of Linux(RHEL8).

Now let’s implement it practically →

Step 1: Writing the Python Code.

Both Client and Server has the similar kind of code.

So, here is the code for same…

Oopsiee😅The code looks difficult if you’re new in socket world. But Lemme make it easy by explaining this:

  • Firstly, we import the socket library because this python library contains the necessary functions to implement sockets. Secondly , we import the threading library for multi-threading. The pyfiglet module is just for displaying the logo.
  • Then, I’ve used AF_NET for IPv4 and SOCK_DGRAM for UDP.
  • After this we need System’s IP and it’s Port Number and Client’s IP and it’s Port Number. You can give Port number as per your choice but make sure that port number is free in your respective System. Then we’ve to bind the sockets.
  • Now, we’ve two functions one for the sender and the other is for the receiver. sendto() function is used for sending the messages and recvfrom() is for receiving the messages.
  • Lastly, using the threading concepts to run both the functions in Parallelism as we’ve to get and receive the data parallelly from both the Server and Client Sides simultaneously.
  • In the above code, we’ve declared an infinite loop so that the application can work continuously.

So Simple, right?😅Yes, it is…

Step 2: Testing the Code.

Run the above code on both Server and Client simultaneously.

And here comes the output of our Chat App 🙈→

Snap of OS1
Snap of OS2

Voila! The Chat App is Created Successfully…

Note: This Demo is based on UDP Protocol and if you try to connect more than one client then the messages will be messed up. For solving this kind of issues, better to use TCP Protocol .

Hope you find my Blog Easy and Interesting!🤞

Do like comment and give a clap👏

That’s all, Signing off👋

Source Code:

--

--

Sumayya Khatoon

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )