Connection refused error in python socket (stream)?
<p>My script is launching multiple clients in a machine-1 and tries to connect to a server running on different machines on a specific port. These machines running the server are launched by boto3 and then wait for 5 min for the instances to be in running state. Then a sftp connection is made to transfer files. Then through ssh the server script is executed. Then when multiple clients are launched in machine-1, and it tries to connect to the server, only one connection established and others got connection refused error <code>ConnectionRefusedError</code>. </p>

<p>Here is my code:</p>

<p>Client:</p>

<pre><code>def Client(datas):
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((datas[0], 81))
    datasent =''+datas[1]+'-'+datas[2]+'#'
    client.send(datasent.encode("UTF-8"))
for dataToRun in dataForMP:
    hp = Process(target=Client, args=(dataToRun,))
    hp.start()
hp.join()
</code></pre>

<p>Server:</p>

<pre><code>if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setblocking(0)
    sock.bind(("", 81))
    sock.listen(128)
</code></pre>

<p>I am launching <code>t1.micro</code> instances for server. I have also waited for 10 min before starting the client and server after launching the instances but I'm getting the same error. </p>

<p>The issue was regarding the time mismatch between starting the server and client. Actually its takes little while to start the server and in the meantime the client request to connect. So I put a 2 sec delay before starting the client and the issue resolved.</p>

python sockets amazon-web-services boto