Write a two way chat application using Socket Programming with
Multithreading in python
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()
Comments
Leave a comment