Chat Server Using UDP Protocol


UDP(User Datagram Protocol) is a standardized method for transferring data between two computers in a network. Compared to other protocols, UDP accomplishes this process in a simple fashion: it sends packets (units of data transmission) directly to a target computer, without establishing a connection first, indicating the order of said packets, or checking whether they arrived as intended.


import socket      #module used for networking#AF_INET used for IPv4#SOCK_DGRAM used for UDP protocol
s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM )
#binding ip and port
#recieving data from client
while True:


import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#sending data to server
while True:
m = input()
s.sendto(m.encode(),("",23)) #sending data in byte form to the server
client and server pc
import socket
import threading
import os
s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM )
s.bind(("",23))#system ip
os.system("tput setaf 3")
print("=======Welcome to the chat page========")
os.system("tput setaf 2")
nm = input("Enter your name : ")
os.system("tput setaf 7")
def send():
while True:
ms = input()
sm = "{} : {}".format(nm,ms)
s.sendto(sm.encode() , ("", 23) ) #server ip
def rec():
while True:
msg = s.recvfrom(1024)
os.system("tput setaf 5")
print("\t\t\t\t" + msg[0].decode() )
os.system("tput setaf 7")
x1 = threading.Thread( target = send )
x2 = threading.Thread( target = rec )
x1.start() #this will start the function at the same time



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