How To Build Python Chat Using UDP And TCP Protocol

Hey guys hope you all are doing good in today's article we are going to see how can we build our own private cha server using python and UDP protocol.

Difference Between UDP and TCP Protocol

Before starting let us understand what is UDP protocol in UDP protocol users send messages no matter if the client is listening or not while in TCP protocol they first go to the client and check if they are listening to them build one session between each other exchange data. Well, that's a high-level idea of UDP and TCP protocol.

Practical implementation of Chat server using UDP protocol

In python, we are going to use socket programming so that we can bind our program to our IP and port so that users can connect.

Receiver Programme UDP Protocol

import socket
myp= socket.SOCK_DGRAM
afn=socket.AF_INET
s=socket.socket(afn,myp)
ip=”192.168.1.75"
port=1234
s.bind((ip,port))
while True:
x=s.recvfrom(1024)
ip = x[1][0]
data = x[0].decode()
print(ip + “ : “ + data)

The above program can be used to receive data via UDP protocol from the server. let's understand prog line by line.

Receiver Programme Explanation UDP Protocol

import socket=> importing necessary module for socket programming
myp= socket.SOCK_DGRAM=> calling protocol that is been used by udp
afn=socket.AF_INET=>calling adress family for udp
s=socket.socket(afn,myp)
ip=”your system ip"
port=any port to which you want your programme to run
s.bind((ip,port))=> binding programme to ip and port
while True:
x=s.recvfrom(1024) => receiving data from client in using whileloop
ip = x[1][0]
data = x[0].decode()
print(ip + “ : “ + data)

Sender Programme UDP Protocol

import socket
server=socket.socket( socket.AF_INET,socket.SOCK_DGRAM)
ip="192.168.1.75"=> change the ip in which recv prog running
port = 1234=>change the port in which recv prog running
server.connect((ip,port))
#print(server.recv(100))
while(True):
a=input()
b=str.encode(a)
server.send(b)

The above program is quite similar to the receiver program we have write. This program is used to send data via UDP protocol.

Now suppose if we want our receiver program to take data from multiple user continuously like it might be the case some client give some data that take time to process and in that time another client also send something so for this we need multiprocessing to need that code you go to my GitHub repo attached at end of the article.

Receiver Programme TCP Protocol

import socket
import threading
server=socket.socket()=> by default uuse tcp protocol
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)=> used to reuse same port
port = 2222
ip=""=> if leave empty will pickup your system ip
server.bind((ip,port))
server.listen()
def ad(session, addr):
print(addr)
session.send(b"Server connected")
while True:
data=session.recv(100)
print(addr[0]+":"+data.decode())
while True:
session , addr = server.accept()
t = threading.Thread(target=ad, args=(session,addr))
t.start()

The above program we are using to receive data from client via TCP protocol in this our program is using multi-process thread to receive data from multiple clients at a time like if suppose some data send by client take 2 min to process and at same time client b send some data then that data will be shown at that time only because our program is using parallel processing using thread module.

Sender Programme TCP Protocol

import socket
server=socket.socket()
ip="192.168.1.75"
port = 2222
server.connect((ip,port))
c=(server.recv(100))
print(c.decode())
while(True):
a=input()
b=str.encode(a)
server.send(b)

The above program is used to send data via TCP protocol to a server.

All the above code is in the Github repo attached at the end of the article.

If you know the basics of python you can do this easily and modify it as per your need.

Hope you like the blog it is not a much big and complex blog but from the basics, we can do great things.

Github Link:-https://github.com/guptaadi123/python-chat-server.git

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store