Chat Server Using UDP Protocol

UDP

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
s.bind(("192.168.1.103",23))
#recieving data from client
while True:
print(s.recvfrom(1024))
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#sending data to server
while True:
m = input()
s.sendto(m.encode(),("192.168.1.103",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(("192.168.1.106",23))#system ip
os.system("tput setaf 3")
print("=======Welcome to the chat page========")
os.system("tput setaf 2")
print("==============================================")
nm = input("Enter your name : ")
os.system("tput setaf 7")
def send():
while True:
ms = input()
sm = "{} : {}".format(nm,ms)
s.sendto(sm.encode() , ("192.168.1.104", 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
x2.start()

--

--

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