Issue when solving DNS using UDP client

Greetings, I’ve been testing a new version of the Ubidots library for the particle core, it seems to work properly in most cases but I experience a strange issue using the photon reference devices when I try to send data through UDP, the next small part of code shows how I build the UDP request to send:


_clientUDP.begin(PORT);
    if (! (_clientUDP.beginPacket(_server, PORT) // server's ip is solved through DNS
        && _clientUDP.write(buffer)
        && _clientUDP.endPacket())){
        if(_debug){
            Serial.println("ERROR sending values with UDP");
        }
        return false;
}

_clientUDP.stop();
free(buffer);

The device begin to send data properly but randomly it simply stops of sending data and the function never returns false. The behavior was too strange but in one test I harcoded the server’s ip and passes it as parameter to the beginPacket() method, making the data never stopped to be sent.

With the above information in mind I made two tests for sending data through TCP and UDP, the common between them is that I manage the network and in some time I block the DNS service so the device would not send data because it can not get properly the IP, below are the results:

TCP:

through TCP once the DNS service is down (refer to the down left console) the data doesn’t arrive to the Ubidots end. When I make it available again, the method connect(_server, PORT) solves successfully the ip of the server and begin to send data properly.

This is the code part for sending data through TCP:


while (!_client.connected() && i < 6) {
        i++;
        if(_debug){
            Serial.println("not connected, trying to connect again");
        }
        _client.connect(_server, PORT);
        if(i==5){
            if(_debug){
                Serial.println("Max attempts to connect reached, data could not be sent");
            }
            return false;
        }
    }
    if (_client.connected()) {        // Connect to the server
        if(_debug){
            Serial.println("Sending data");
        }
        _client.println(buffer);
        _client.flush();
_client.stop();

UDP:

The same test as above was done, once the DNS goes down the data sending stops as expected, but when it is available again the method to begin to send packets doesn’t solve properly the DNS (but still follow making requests to solve the domain, so I think that it gets the ip but this one is never updated) and doesn’t return an error so the data simply never goes to the end server.

What I believe is that the randomly stops come when the photon for any reason gets a wrong DNS answer or simply didn’t get it and the memory register is not updated every time that the beginPacket() method is called.

Is there any issue with the beginPacket() method or it should not be used for solving domain names and we must to input a harcoded ip address?

Thanks in advice.

1 Like

@jotathebest, the TCP client can resolve an IP form a hostname but the UDP client cannot. You may want to use the WiFi.resolved(hostname) function to resolve (or not) your host IP prior to using your UDP client since you can test for a non-resolved IP condition (IPaddress = 0.0.0.0). That way, you can avoid making the client calls to begin with and wait for a valid host IP.