Chat Program using UDP with and without multi-threading

Arya Dhorajiya
2 min readJan 16, 2021

--

ARTH β€” Task 17 πŸ‘¨πŸ»β€πŸ’»

Task DescriptionπŸ“„

πŸ”…-17.1 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 UDP data transfer protocol.

πŸ”…-17.2 Use 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.

Task 1 :- Creating Chat Servers via UDP Protocol

So I created a Server and Client using Socket Programming and got the output as follows :-

Server Side :-

Client Side :-

Task 2 :- Using Multi-threading to achieve it…

While in Multi-threading, the code will be a bit different as we will use threading module, which will help read and send message at the same time. The overall output will be similar but the code will be different.

β€œ import socket

import threading

import os

import time

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

print(β€œ β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” \n”)

print(β€œ\t\tChat Application using multithreading”)

print(β€œ β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” \n”) print(β€œHii…. This is WINDOWS10\n”)

ip = input(β€œ\n\t\tEnter Your IP : β€œ)

port =int(input(β€œ\n\tEnter your port number :”))

clientip = input(β€œ\n\t\tEnter Sender IP : β€œ)

clientport =int(input(β€œ\n\tEnter sender port number :”))

s.bind( (ip,port) )

def send():

while True:

msg = input(β€œYour Message: β€œ).encode() s.sendto(msg,(clientip,clientport)) if msg.decode() == β€œexit” or msg.decode() == β€œquit”: os._exit(1)

def recv():

while True:

msg = s.recvfrom(1024) if msg[0].decode() == β€œquit” or msg[0].decode() == β€œexit”: os._exit(1) print(β€˜\n\t\t\t\t\t\tReceived Msg: β€˜+ msg[0].decode())

t1 = threading.Thread(target=recv)

t2 = threading.Thread(target=send)

t1.start()

t2.start() β€œ

So, this is the Threading code and it will help execute multi-threading in a program.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arya Dhorajiya
Arya Dhorajiya

No responses yet

Write a response