Answer to Question #254041 in Python for shishi

Question #254041

Write a two way chat application using Socket Programming with

Multithreading in python


1
Expert's answer
2021-10-23T03:04:10-0400

server side

import socket
from _thread import *
import threading


plock = threading.Lock()


def threaded(cur):
	while True:
		d = cur.recv(1024)
		if not d:
			plock.release()
			break
		d = d[::-1]
		cur.send(d)
	cur.close()


host = ""
port = 55555
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.bind((host, port))
soc.listen(5)
while True:
	cur, address = soc.accept()
	plock.acquire()
	print('Connected to :', address[0], ':', address[1])
	start_new_thread(threaded, (cur,))
soc.close()

client side



import socket
host = '127.0.0.1'
port = 55555
soc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
soc.connect((host,port))
message = "Hello world"
while True:
	soc.send(message.encode('ascii'))
	d = soc.recv(1024)
	print('Received from the server :',str(d.decode('ascii')))
	c = input('\nContinue(y/n) :')
	if c == 'y':
		continue
	else:
		break
s.close()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS