0

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?

Kris
  • 8,680
  • 4
  • 39
  • 67
harika
  • 71
  • 7
  • You are breaking at line 39 from `for` loop, but not from wrapping `while` loop . Does that sound like a problem ? – Kris Dec 30 '21 at 13:50
  • No, even I tried removing that break it's still not repeating the while loop – harika Dec 30 '21 at 13:51
  • It will not work by removing `break`, basically you have to add a break for the while loop around your for. btw, is that `while True:` really required ? – Kris Dec 30 '21 at 13:59
  • yes, server will receive the response in batches. so It need to know when to stop expecting for the response – harika Dec 30 '21 at 14:14
  • Not sure why you create a thread for each client. Try just creating one thread and run the server on that thread. Run the input loop on the main thread. – 001 Dec 30 '21 at 14:35
  • Also, `sendto` is used on UDP sockets. User `send` for TCP. – 001 Dec 30 '21 at 14:36
  • Do not use `send` unless you're checking the return value and prepared to do something if it isn't what you expect. Instead, use `sendall`. But you're entire concept of the way streaming sockets work as illustrated by `connectionList[i].recv(2048)` is flawed. Please read [this answer](https://stackoverflow.com/a/43420503/238704) to understand why and how to fix it. – President James K. Polk Dec 30 '21 at 14:48

0 Answers0