Currently, my code only allows a finite number of socket connections. This is determined by the variable No_Of_Connections.
My question is: how do I make it such that it is not finite? Meaning I do not have to hard code the number of connections. I would also need to be able to terminate or join the thread when the client closes the connection.
Any help is greatly appreciated. Thank you!
def Main():
HOST = '192.168.2.9'
PORT = 65533
No_Of_Connections = 10
trds = []
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(No_Of_Connections)
print("Server is running on port: " + str(PORT))
try:
for i in range(No_Of_Connections):
c, addr = s.accept()
clients.append(c)
t = Thread(target=clientHandler, args=(s, c, addr))
trds.append(t)
t.start()
for t in trds:
t.join()
except KeyboardInterrupt:
print("caught keyboard interrupt in main, exiting")
s.close()
except Exception as e:
print('Socket Server error: ' + str(e))