Oct-16-2020, 02:58 PM
Hi
I have made a multithreaded Python socket server.
It supports multiple socket client connections. However, it serves the client only synchronously. If one client is connected to it, then the other client needs to wait until the current clients data sending/receiving to/from socket server is done.
My code is:
May be, the Socket server should run multiple instances of itself for each new connection made by a client. (Just guessing!)
2) Also,
initially the socket server has 0 clients connected to it.
I want that when some clients are connected to it & if all clients disconnect from it, then it should get closed automatically.
I am unable to figure out these.
Kindly help
Thanks
I have made a multithreaded Python socket server.
It supports multiple socket client connections. However, it serves the client only synchronously. If one client is connected to it, then the other client needs to wait until the current clients data sending/receiving to/from socket server is done.
My code is:
import socket from _thread import * def multi_threaded_client(connection): response = '' while True: data = connection.recv(10000000) response += data.decode('utf-8') if not data: print(response) break connection.send(bytes(some_function_name(response), "utf-8")) connection.close() class socketserver: def __init__(self, address='', port=9090): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.address = address self.port = port self.sock.bind((self.address, self.port)) self.cummdata = '' def recvmsg(self): self.sock.listen(65535) self.conn, self.addr = self.sock.accept() print('connected to', self.addr) self.cummdata = '' start_new_thread(multi_threaded_client, (self.conn, )) return self.cummdata def __del__(self): self.sock.close() serv = socketserver('127.0.0.1', 9090) print('Socket Created at {}. Waiting for client..'.format(serv.sock.getsockname())) while True: msg = serv.recvmsg()1) I would like it to work parallely, but I am stuck here, since I am quite new to all these topics
May be, the Socket server should run multiple instances of itself for each new connection made by a client. (Just guessing!)
2) Also,
initially the socket server has 0 clients connected to it.
I want that when some clients are connected to it & if all clients disconnect from it, then it should get closed automatically.
I am unable to figure out these.
Kindly help
Thanks