Chat Program using UDP with and without multi-threading

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Three Tricks on Python Functions that You Should Know

Music Reactive Video Case Study

DevSecOps in layman’s terms

I would like to thank you for your airdrop.

MUGA Pitch Contractors in Sacombe Green #MUGA #Court #Contractors #Sacombe #Green https://t.co/gV8sH

Tint: Multi-Cloud Cost Visualization for Terraform

10 Convincing Reasons to Choose Kotlin for App Development

Jwt Authentication In ASP.NET Core Web API

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
Arya Dhorajiya

Arya Dhorajiya

More from Medium

Line-by-line coding: Advanced python quiz game

Get Started with Django in 5 min

TIL Upload Image Files

TensorFlow Developer Certificate