The Python Chat-Server

Divyanshu Sharma
4 min readMar 10, 2021

Let us chat…

Communication has invariably been vital in the social life of humans. The mode of communication is evolving over the decade. This evolvement has brought in the crispiest concept of chatting.

Chatting has unquestionably walked into everyday life so much so that no one is deprived of it. From messenger to text to WhatsApp to what not has been used for the same.

Ever wondered how to send these messages ?? Or let me ask you can you create these chat servers on your personalized computers?

And to this what I have is a big fat yes.

This article gives a synopsis as to how you can create the chat-server using python. To this, we will understand some terms and then design a code for the same.

What is Socket Programming?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. They are the real backbones behind web browsing. In simpler terms, there is a server and a client.

The Flow of the socket programming

What is a Thread?
A thread is a light-weight process that does not require much memory overhead, they are cheaper than processes.

What is Multi-threading Socket Programming?
Multithreading is a process of executing multiple threads simultaneously in a single process.A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

What is UDP?

UDP socket routines enable simple IP communication using the user datagram protocol (UDP).UDP is a very simple protocol. Messages, so-called datagrams, are sent to other hosts on an IP network without the need to set up special transmission channels or data paths beforehand.

UDP is inherently unreliable in the sense that: UDP packets may be lost, and. the UDP protocol provides no mechanism to tell if packets have been lost, or to resend them. There we use the concept of Multi threading to built the connection between two system.

Let Us Code

Observing the given terms we can now drive a code for the same.

Firstly import the libraries to perform socket programing. Write the same code on both servers. The receiver function means the os where the code is running and the sender function is the other server. For standard sockets programming, the first argument is always AF_INET. The second argument is typically SOCK_DGRAM for UDP. Create the two function receiver and the sender.

(SOCK_DGRAM) the interface defines a connectionless service for datagrams or messages. Datagrams are sent as independent packets.

AF_INET is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses)

Now as mentioned we add the two IPs and their port respectively. The next step is to bind them and run the thread in the end.

Here, the IP has been hardcoded. You can use the following function to make it dynamic.

Code for dynamic

Output

This is the output of the code generated in two different servers.

Dynamic Ip using multi-thread
Hardcoded IP using UDP

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Divyanshu Sharma
Divyanshu Sharma

Written by Divyanshu Sharma

Are you reading ? Cause I am writing :)

No responses yet

Write a response