chat app using udp protocol

Pravat kumar Nath sharma
2 min readMay 31, 2021

User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a part of Internet Protocol suite, referred as UDP/IP suite. Unlike TCP, it is unreliable and connectionless protocol. So, there is no need to establish connection prior to data transfer.

Though Transmission Control Protocol (TCP) is the dominant transport layer protocol used with most of Internet services; provides assured delivery, reliability and much more but all these services cost us with additional overhead and latency. Here, UDP comes into picture. For the realtime services like computer gaming, voice or video communication, live conferences; we need UDP. Since high performance is needed, UDP permits packets to be dropped instead of processing delayed packets. There is no error checking in UDP, so it also save bandwidth.
User Datagram Protocol (UDP) is more efficient in terms of both latency and bandwidth.

UDP Header –

UDP header is 8-bytes fixed and simple header, while for TCP it may vary from 20 bytes to 60 bytes. First 8 Bytes contains all necessary header information and remaining part consist of data. UDP port number fields are each 16 bits long, therefore range for port numbers defined from 0 to 65535; port number 0 is reserved. Port numbers help to distinguish different user requests or process.

  1. we use socket program and udp protocol for communication
  2. create a python program for client and server

import socket as soc
import os
import threading
import pyfiglet

## your ip
SenderIP = input(“Enter Sender IP address: “)
SenderPort = int(input(“Enter Sender port: “))

## Friends ip
ReciverIP = input(“Enter Reciver IP address: “)
ReciverPort = int(input(“Enter Reciver port: “))

## Creating UDP Socket
ReadySocket = soc.socket(soc.AF_INET,soc.SOCK_DGRAM)

## only for Reciving messages
ReadySocket.bind((SenderIP,SenderPort))

os.system(‘cls’) ## For linux use os.system(‘clear’)
print(pyfiglet.figlet_format(‘Walkers Chat’))

## Sending message function
def SendMsg():
while True:
Message = input()
print(“\n”)
if(Message == ‘quite’ or Message == ‘bye’ or Message == ‘exit’):
Message = ‘Your Friend is offline’
ReadySocket.sendto(Message.encode(),(ReciverIP,ReciverPort))
os._exit(1)
else:
ReadySocket.sendto(Message.encode(),(ReciverIP,ReciverPort))

## Receving message function
def RecvMsg():
while True:
Msg = ReadySocket.recvfrom(100)
print(“\n\t\t\t\t” + Msg[1][0] + “:” + Msg[0].decode())

## Created 2 Threads for sending and reciving msg parllerl
SendMsg = threading.Thread(target=SendMsg)
RecvMsg = th

reading.Thread(target=RecvMsg)

## starting thread
SendMsg.start()
RecvMsg.start()

for simultaneously chat we use multithreading

multithreading:

Multithreading is a threading technique in Python programming to run multiple threads concurrently by rapidly switching between threads with a CPU help (called context switching).

this is client machine

--

--