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)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)#make udp socket and use it to connect
STARTTIME = time.time()
prevsent = STARTTIME
print "Node started..."
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:
msgArr = forwardQueue.pop(0)
for sock in readable: #if there's something to be read...
recvMsg, addr = sock.recvfrom(2048)
print "ugly disconnect detected" + str(addr) + recvMsg
for sock in err:
if time.time() - MINI_UPDATE_INTERVAL > prevsent: #once a second
# print time.time() - STARTTIME
for key,value in dictMap.iteritems():
I believe that you will get your error each time you try to send a packet to a port on a host where nothing is listening on that port (or the listen backlog is full).
I suggest removing the entry from dictMap in the exception handler to stop additional exceptions from being thrown, since you now know nothing is there.