Zoroc1
Zoroc1

Reputation: 1

Python 3 Threading Using Two Ports

i am working on a basic python program to get used to threading and networking and i have become a little unstuck at one section of my code.

what i have is:

#make a socket and loop to obtain connections 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ads = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind(("127.0.0.1" , 4000))
ads.bind(("127.0.0.1" , 4001))

s.listen(10)
ads.listen(1)

socks = [s,ads]
connections = [] # list of connections 

while True:
    if ads:
        (c,a) = ads.accept()
        t = threading.Thread(target = admin_client, args = ())
        t.start()
    elif :   
        (c,a) = s.accept()
        connections.append(c)
        t = threading.Thread(target = handle_client, args = (c,a))
        t.start()

What i was hoping to happen was when the ads port was accessed it would assign it to the admin_client method which it seems to perform but it will just do nothing if anything connects on the s port.

Does anyone have a solution for this so both clients will connect with no issues?

Upvotes: 0

Views: 87

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177725

if ads: is always True. You need to use select. Since if ads: is always True you drop into (c,a) = ads.accept() which waits for someone to connect to the ads port.

Something like (untested):

r,w,x = select.select(socks,[],[])
if ads in r:
     ...
elif s in r:
     ...

Upvotes: 1

Related Questions