CHAT APPLICATION USING THE CONCEPT OF SOCKET PROGRAMMING AND MULTI-THREADING IN PYTHON :
Hi Everyone✌️✌️
Today I came up with a chatting application using UDP protocol written in python as per the task
🔸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.
📌 Use the multi-threading concept to get and receive data parallelly from both the Server Sides. Observe the challenges that you face to achieve this using UDP.
First, let us see about some basic terms of this application like…
🔅 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 osos.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 IPdef 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()