I've been mucking around with a program that sends packets to other copies of itself, and recvfrom has been behaving in a way I don't fully understand. Each instance of the program is set up on a different port (with knowledge of other instances' port numbers already stored in the dictMap dictionary). The idea is that after I've started up a number of instances of this program (say, 8), they should all be pinging each other 3 times a second (MINI_UPDATE_INTERVAL).
However, if I close one of the instances while a whole bunch are running, the programs all keep printing 'ugly disconnect detected etc.' multiple times, even though the instance disconnected has only disconnected once. What's the reason behind this? A portion of my code is below:
PORT = int(args.port)
socket.setdefaulttimeout(1)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)#make udp socket and use it to connect
s.bind(("",PORT))
s.setblocking(False)
#timing considerations
STARTTIME = time.time()
prevsent = STARTTIME
print "Node started..."
while True:
readable, writable, err = select.select([s],[s],[s]) #using select to poll our socket to see if it is readable
for sock in writable:#if s is writable (it should be), there are packets in the send queue AND one of the next two conditions is met:
if forwardQueue:
msgArr = forwardQueue.pop(0)
sock.sendto(msgArr[MSG],("127.0.0.1",int(msgArr[DESTPORT])))
for sock in readable: #if there's something to be read...
try:
recvMsg, addr = sock.recvfrom(2048)
except:
print "ugly disconnect detected" + str(addr[1]) + recvMsg
break
for sock in err:
print "ERROR"
if time.time() - MINI_UPDATE_INTERVAL > prevsent: #once a second
# print time.time() - STARTTIME
for key,value in dictMap.iteritems():
forwardQueue.append([('PING'+ '|'+idName+'|'+str(time.time())),value])
EDIT: The problem seems to only occur on windows (where WSAECONNRESET constantly keeps popping up after a disconnect). Since my code is ultimately destined for linux I guess it's no big deal.
答案 0 :(得分:1)
我相信每次尝试将数据包发送到主机上的端口时都会收到错误,而该主机上没有任何内容正在侦听该端口(或者监听积压已满)。
我建议在异常处理程序中从dictMap中删除条目,以阻止抛出其他异常,因为你现在什么都不知道了。