1

i am trying to establish connections for multiple sockets using multi-threading

this is the code

import socket as sc
if __name__ == '__main__':


    #setup()
    ports = [10000, 10010, 10020, 10030]

    init_sockets()
    init_threads()
def init_sockets():

    global host_ip
    global sockets

    host_ip = sc.gethostname()
    sockets = []

    for port in ports:

        socket = sc.socket()
        socket.bind((host_ip, port))
        socket.listen()
        sockets.append(socket)
def init_threads():

    threads = [
        threading.Thread(target= init_connection, args= [socket])
        for socket in sockets
    ]

    for thread in threads:
        thread.start()
def init_connection(socket):

    client, address = socket.accept()

while running the code this error appears

ConnectionAbortedError: [Errno 53] Software caused connection abort

the error occurs in thread.start() statement in function init_threads()

i don't know why this is happening, and would really appreciate any help. i am trying to run multiple socket connections in parallel, if it's impossible this way, i am open to recommendations

hammi
  • 804
  • 5
  • 14
  • The code seems to be fine. I had an issue with the host name, but changing it to 127.0.0.1 solved it. Make sure one of the ports isn't used by some other application, – Roy2012 Jun 17 '20 at 17:32

1 Answers1

0

solved it!

the problem seemed to be that when the main thread (program it self) is terminated, all the objects that it created are deleted, including the socket objects.

so when the secondary threads (threads that the main program started), still running, try to reference these deleted objects the error occurs

the solution for me appeared to be adding an infinite while loop in the main method. preventing the main thread from being terminated

hammi
  • 804
  • 5
  • 14