简体   繁体   English

Python UDP程序与select的多个连接

[英]Python UDP program multiple connections with select

I'm making a UDP program that connects multiple clients/neighbors. 我正在制作一个连接多个客户端/邻居的UDP程序。 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. 我正在尝试使用localhost进行测试,因此请考虑邻居的所有IP和端口均按预期工作。 Using 127.0.0.1 as IP on all and connecting to different ports. 全部使用127.0.0.1作为IP并连接到不同的端口。 So my question is why do I receive the startup data that I send before the while loop but I cannot send any? 所以我的问题是,为什么我会收到在while循环之前发送的启动数据,但无法发送任何启动数据? Seems that I am doing something wrong with sys.stdin. 似乎我对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. input始终为[s] ,因此i始终将为[s] ,因此x == sys.stdin永远不会为True (因为x始终为s ),因此仅else子句将始终执行。

Maybe you meant input = [s, sys.stdin] ? 也许您是说input = [s, sys.stdin]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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