CHAT APPLICATION USING THE CONCEPT OF SOCKET PROGRAMMING AND MULTI-THREADING IN PYTHON :

🔸Task Description🔸

📌 Create your own Chat Servers and establish a network to transfer data using Socket Programing by creating both Server and Client machine as Sender and Receiver both. Do this program using the UDP data transfer protocol.

🔅 UDP Protocol

User Datagram Protocol (UDP) is a communications protocol that facilitates the exchange of messages between computing devices in a network. It’s an alternative to the transmission control protocol (TCP). In a network that uses the Internet Protocol (IP), it is sometimes referred to as UDP/IP.

🔅 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.

🔅Multi Threading

Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core

✍️Code:-

import pyfiglet # For showing the letters on command terminal
import threading # For enabling the sending and receiving the messages at the same time
import socket # for sending the messages through the sockets
import os
os.system("clear")title = pyfiglet.figlet_format("Welcome to Chat Program", font ="digital")
print(title)
print("|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|")
print()
YourIP = input("Enter your IP :-")
YourPort = int(input("Enter your PortNumber :-"))
FrndIP = input("Enter your Friend IP :-")
FrndPort = int(input("Enter your Friend PortNumber :-"))
# This helps to send the messages to our friend IP
def sender():
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
print("You:-",end='')
x = input().encode()
s.sendto(x,(FrndIP,FrndPort))
if x.decode()== "exit" or x.decode() == "bye":
os._exit(1)
# This helps to receive the messages from our frienddef reciver():
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind((YourIP,YourPort))
while True:
x = s.recvfrom(1024)
if x[0].decode() == "exit":
print('\n',end='')
os._exit(1)
else:
print("\n\t\t\tYourfrnd:-"+x[0].decode()+"\nYou:-",end='')
# This is for assigning two threads for two functions(sender, receiver )
y1 = threading.Thread( target=sender )
y2 = threading.Thread( target=reciver )
# This is for starting the threadsy1.start()
y2.start()

👉Chat between two computers:-

⚜️ Here is a small video that I have created for visual experience👇

Thank you for visiting my blog 😊

--

--

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