0

I am experiencing slowdowns when attempting to send a UDP message to an IP address that is non-existent. We read a list of IP/ports out of a configuration file and expect that those combinations will exist. In some situations, the remote machine may not be online yet and the IP will not exist on the network. In this case, i would prefer that we do not attempt to send the data.

I'm looking for suggestions on a good way to determine that the IP doesn't exist in order to skip sending the data. I do not want to completely remove it from the list because there is the chance that the machine may come online and i'd like to continue sending data.

Relevant code:

int SendData(const char *data, int size, bool openIfClosed)
    {
        std::lock_guard<std::mutex> lock(sendMutex);        

        //if the socket was not already opened, close it after i do my send
        bool closeAfterSend = mySocket == INVALID_SOCKET ? true : false;

        if (!OpenSocket(openIfClosed))                              
            return 0;

        sockaddr_in address;
        address.sin_family = AF_INET;
        address.sin_port = htons(remotePort);
        address.sin_addr.s_addr = remoteIPAddress.GetIPAddressAsULong();             

        //check if remote address exists prior to send?
        int bytesSent = sendto(mySocket, data, 
                        size, 0, (struct sockaddr *)&address, sizeof(address));
        if (bytesSent == size)
            numMsgsOut++;
        else
        {
           //fail
        }


        if (closeAfterSend && openIfClosed)
        {
            closesocket(mySocket);
            mySocket = INVALID_SOCKET;
        }

        return bytesSent;
    }
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Jason
  • 2,147
  • 6
  • 32
  • 40
  • 3
    Maybe send some kind of "are you there?" message to all hosts on regular intervals, and those that doesn't reply are moved to a separate list that is not used to send data, only the regular "are you there?" message. – Some programmer dude Aug 10 '17 at 13:34
  • IP addresses are not "non-existent". What they are used for and whether a packet sent towards it will reach something (and whether that something will choose to reply) is another question. One that cannot be answered without actually sending the packet and waiting for an answer. You will experience slowdowns if your code **waits** for one server before proceeding. Moving to a parallel approach will overcome that. – spectras Aug 10 '17 at 13:34
  • @spectras - I don't disagree. In this particular case, there is a design flaw that exposed the issue. While i can fix this particular design flaw somewhat, the message rate would force a backlog of messages and that is not exactly ideal either. My thought was to evaluate the length of time to send. If it is beyond a threshold i designate, i begin pinging the ip prior to sending. If the ping works, then i turn it back off and send normally. – Jason Aug 10 '17 at 13:56
  • Are the destination IP addresses on the local subnet? Perhaps the slowdown is caused by the ARP layer sending out broadcast packets, trying (unsuccessfully) to find the physical location (MAC address) of the machine with the specified IP address. – Jeremy Friesner Aug 10 '17 at 14:13
  • @JeremyFriesner - that is what i believe is happening. My program works on a lan, and they are on the same subnet. My research has led me to believe that the ARP layer is causing the delay. – Jason Aug 10 '17 at 14:18
  • 3
    I think @Someprogrammerdude has the solution; you need to keep a list of currently-online hosts and only send() to the IP addresses that are on that list. Easiest way to do that would be to send out a single broadcast packet, e.g. once every 10 seconds, and have all hosts reply to that packet (with a unicast reply). Keep a table of IPAddress->timestamp showing when each IP address was most recently heard from, and only send() data to an IP address if the table indicates that it has been heard from within the last (so many) seconds. – Jeremy Friesner Aug 10 '17 at 14:24
  • @JeremyFriesner - That is exactly what i am going to do! Thanks for the help. – Jason Aug 10 '17 at 15:21

0 Answers0