2

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.

  • Not sure where you think a disconnect message is coming from on a udp socket. There is no connection, so there can be no disconnection. Maybe you aren't getting the exception you think you are? Why don't you actually grab the exception and print out its details? – xaxxon Oct 20 '16 at 13:15
  • when I print out the exception I get errno 10054 (existing connection closed by remote host) – Thomas Kilkelly Oct 20 '16 at 13:35

1 Answers1

1

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.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • In the actual code I do remove disconnected entries from dictMap, but the exceptions keep getting thrown even though the offending ports are no longer sending anything at all. – Thomas Kilkelly Oct 20 '16 at 13:53
  • 1
    is it possible you have multiple sends in flight already when the other side goes down? Also, you should probably call recvfrom in a loop until it returns a EWOULDBLOCK... calling select is expensive -- right now you will only get one result for each time you call select, even if there are 100 available. – xaxxon Oct 20 '16 at 13:53
  • Very possible. Is there any way for me to see if the socket buffer is full (or how full it currently is)? – Thomas Kilkelly Oct 20 '16 at 13:54
  • Also, this is on windows, isn't it? This seems to be a windows thing. – xaxxon Oct 20 '16 at 13:55
  • This is on windows. This is for an assignment that ultimately is getting demoed on a linux machine (I'll be heading into uni to test that tomorrow), but I'll give your suggestion a go right now – Thomas Kilkelly Oct 20 '16 at 13:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126238/discussion-between-thomas-kilkelly-and-xaxxon). – Thomas Kilkelly Oct 20 '16 at 14:12