✍TO CREATE A CHAT APPLICATION USING THE CONCEPT OF SOCKET PROGRAMMING AND MULTI-THREADING IN PYTHON :

HELLO EVERYONE ✌✌,

✍ TASK DESCRIPTIONS :

✍WHAT IS UDP PROTOCOL ?

UDP PROTOCOL

✍APPLICATION OF UDP :

✍UDP COMMUNICATION :

UDP COMMUNICATION

✍WHAT IS SOCKET PROGRAMMING ?

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

✍WHAT IS Recvfrom FUNCTION IN SOCKET PROGRAMMING ?

✍WHAT IS THREAD ?

MULTI-THREADING IN PYTHON

✍WHAT IS MULTI-THREADING IN PYTHON ?

Before doing any practical implementations It’s a good practice to create one seperate directory to store all the files.

👉SO, WE HAVE TO WRITE A PYTHON PROGRAM IN WINDOWS FOR CHAT APPLICATION :

# cd TASK17

# notepad chat_windows.py

👉NEXT :

✍CODE :

import socket as soc
import threading as thread
import os
import pyfiglet
os.system("cls")
os.system("color 6")
title = pyfiglet.figlet_format("\t ! WELCOME TO MY UDP CHAT APP FROM WINDOWS ! \n")
print( title)
print(" \n ##################################################################### \n")
print("\t\t SO FRIEND LET'S START OUR CHATTING \n")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
your_ip = input("ENTER THE YOUR IP ADDRESS HERE : ")
print("\n")
your_port = int(input("ENTER THE YOUR PORT NUMBER HERE : "))
print("\n")
frd_ip = input("ENTER THE FRIEND IP ADDRESS HERE : ")
print("\n")
frd_port = int(input("ENTER THE FRIEND PORT NUMBER HERE : "))
print("\n")
# TO CREATE A SOCKET AND BIND THE IP AND PORT NUMBER :skt1 = soc.socket(soc.AF_INET, soc.SOCK_DGRAM)
skt1.bind((your_ip, your_port))
# WE CAN USE THIS FUNCTION TO RECEIVING AND PRINTING THE MESSAGE :def recieve_msg():
while True:
os.system("color 5")
msgRcv = skt1.recvfrom(1024)
if msgRcv[0].decode() == "quit" or msgRcv[0].decode() == "bye bye" or msgRcv[0].decode() == "exit":
print("NOW YOUR FRIEND GOES OFFLINE!")
os._exit(1)
print("\n\t\t\t YOUR FRIEND'S MSG --->" + msgRcv[0].decode())
# WE CAN USE THIS FUNCTION TO SENDING THE MESSAGE :def send_msg():
while True:
data = input().encode()
msgSent = skt1.sendto(data, (frd_ip, frd_port))
if data.decode() == "quit" or data.decode() == "bye bye" or data.decode() == "exit":
os._exit(1)
# WE CAN USE THIS THREAD FOR SENDING THE MESSAGE FUNCTION :t1= thread.Thread(target=send_msg)# WE CAN USE THIS THREAD FOR RECIEVING THE MESSAGE FUNCTION :t2 = thread.Thread(target=recieve_msg)# WE CAN USE THIS FUNCTION TO STARTING OUR THREADS :
t1.start()
t2.start()

👉SO, WE HAVE TO WRITE A PYTHON PROGRAM IN LINUX FOR CHAT APPLICATION :

# cd TASK17# gedit chat-linux.py

✍NEXT :

✍NEXT :

✍CODE :

import socket as soc
import threading as thread
import os
import pyfiglet
os.system("clear")
os.system("tput setaf 6")
title = pyfiglet.figlet_format("\t\t! WELCOME TO MY UDP CHAT APP FROM LINUX ! \n")
print(title)
print("\n ####################################################################### \n")
print("\n\t\tSO FRIEND LET'S START CHATTING \n")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
your_ip = input("ENTER THE YOUR IP ADDRESS HERE : ")your_port = int(input("ENTER THE YOUR PORT NUMBER HERE : "))frd_ip = input("ENTER THE FRIEND IP ADDRESS HERE : ")frd_port = int(input("ENTER THE FRIEND PORT NUMBER HERE : "))# TO CREATE A SOCKET AND BIND IP AND PORT NUMBER :skt2 = soc.socket(soc.AF_INET, soc.SOCK_DGRAM)
skt2.bind((your_ip, your_port))
# WE CAN USE THIS FUNCTION TO RECIEVING AND PRINTING THE MESSAGE :def recieve_msg():
while True:
os.system("tput setaf 2")
msgRcv = skt2.recvfrom(1024)
if msgRcv[0].decode() == "quit" or msgRcv[0].decode() == "bye bye" or msgRcv[0].decode() == "exit":
print("NOW YOUR FRIEND GOES OFFLINE.....")
os._exit(1)
print("\n\t\t\t YOUR FRIEND'S MSG --->" + msgRcv[0].decode())
# WE CAN USE THIS FUNCTION TO SENDING THE MESSAGE :def send_msg():
while True:
data = input().encode()
msgSent = skt2.sendto(data, (frd_ip, frd_port))
if data.decode() == "quit" or data.decode() == "bye bye" or data.decode() == "exit":
os._exit(1)
# WE CAN USE THIS THREAD FOR SENDING THE MESSAGE FUNCTION :t3 = thread.Thread(target=send_msg)# WE CAN USE THIS THREAD FOR RECIVING THE MESSAGE FUNCTION :t4 = thread.Thread(target=recieve_msg)# WE CAN USE THIS FUNCTION TO STARTING OUR THREADS :t3.start()
t4.start()

👉NOW, I HAVE TO RUN BOTH THE PROGRAMS SIMULTANEOUSLY :

# python chat_windows.py   [ FOR WINDOWS OS ]# pythons chat_linux.py    [ FOR LINUX OS ]

👉NOW, YOU CAN SEE THAT I AM ABLE TO CHAT BETWEEN TWO DIFFERENT TYPES OF OS.

✍CONCLUSION :

✍SOLUTION :

SEE YOU IN THE NEXT BLOG WITH MORE AMAZING TASKS.

THANK YOU🙏🙏 GUYS FOR READING MY BLOG…

SIGNING OF FORM MY SIDE 👋👋

KEEP LEARNING🙇‍♂️📖🙇….

KEEP SHARING✌✌….

ARTH-LEARNER || IIEC RISE || FLUTTER || PYTHON || REDHAT || DOCKERS || AI || BIG DATA || HADOOP || ANSIBLE || KUBERNETES || ML || DL || JENKINS || OPENSHIFT ||