简体   繁体   中英

Python UDP program multiple connections with select

I'm making a UDP program that connects multiple clients/neighbors. There is no server everyone is using the same program. I'm trying to test it with localhost so consider all IPs and ports of neighbors work as intended. Using 127.0.0.1 as IP on all and connecting to different ports. So my question is why do I receive the startup data that I send before the while loop but I cannot send any? Seems that I am doing something wrong with sys.stdin.

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST, PORT))
going = True

input = [s]
while going:

  i,o,e = select.select(input,[],[],)
  try :
    for x in i:
      if x == sys.stdin: 
        data = sys.stdin.read()
        for i in neighbors:
          addr = (i[1][0], int(i[1][1]))
          s.sendto(data, addr)
      else:
        msg, addr = s.recvfrom(100)
        print msg
  except socket.error, msg:
    print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()   
s.close()

input is always [s] , so i is always going to be [s] , so x == sys.stdin is never going to be True (because x is always s ), so only the else clause will ever execute.

Maybe you meant input = [s, sys.stdin] ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM