I'm trying to send the command from the server to the connected clients and trying to receive the response.
But connectionList[i].recv(2048)
breaking the while loop. I want the server to ask for the "Enter the command" repeatedly. but the loop is not repeating once it's receiving the response from clients
Here is my code
import socket
import sys
from _thread import *
import threading
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
host = socket.gethostname()
server_address = ("0.0.0.0", 8765)
sock.bind(server_address)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#Listen for incoming connections
sock.listen(5)
print ("Waiting for connection...")
#Variable for the number of connections
numbOfConn = 0
ThreadCount = 0
#Name of list used for connections
addressList = []
connectionList = []
#Function that continuosly searches for connections
def clients(connectionList, addressList):
while True:
cmd=input("Enter input command:")
#for loop to send message to each
for i in range(0,numbOfConn):
connectionList[i].sendto(str.encode(cmd), addressList[i])
while True:
for i in range(0,numbOfConn):
response = connectionList[i].recv(2048)
#break
print("response")
if(response.decode('utf-8') =="done"):
print("Exiting the loop")
break
#connection.close()
while True:
#accept a connection
connection, address = sock.accept()
print ('Got connection from', address)
numbOfConn += 1
addressList.append((address))
connectionList.append((connection))
#Thread that calls the function: clients and stores them in a tuple called connection
start_new_thread(clients, (connectionList, addressList))
ThreadCount += 1
print('Thread Number: ' + str(ThreadCount))
sock.close()
could you please help me to find out the issue?