Значения client.connect(server, port)

Вячеслав21
Offline
Зарегистрирован: 29.05.2015

В интернете нашел, что функция client.connect(server, port) может принимать значения:

Returns

Returns an int (1,-1,-2,-3,-4) indicating connection status : SUCCESS 1 TIMED_OUT -1 INVALID_SERVER -2 TRUNCATED -3 INVALID_RESPONSE -4

Подскажите, если функция возвращает -5, что это может означать?

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
char server[] = {192, 168, 1, 2};   

byte ip[] = { 192, 168, 1, 3 };


EthernetClient client;

String currentLine = "";

void setup() {
 

   Ethernet.begin(mac, ip);
    Serial.begin(9600);
 }
  


void loop()
{
String msg = "GET /test2.php HTTP/1.0";           
                   
Serial.println(msg);
 int s = client.connect(server, 80);
 Serial.println(s);
delay(1000);
}

 

Araris
Offline
Зарегистрирован: 09.11.2012

В исходниках библиотеки :

return -5; //INVALID_RESPONSE;

Имеется в виду DNS RESPONSE.

Вячеслав21
Offline
Зарегистрирован: 29.05.2015

Спасибо.

ЕвгенийП
ЕвгенийП аватар
Offline
Зарегистрирован: 25.05.2015

Там бывает не только -5, а вплоть до -10. См. текст, там по комментариям можно всё понять, что за коды такие:

uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
{
    uint32_t startTime = millis();

    // Wait for a response packet
    while(iUdp.parsePacket() <= 0)
    {
        if((millis() - startTime) > aTimeout)
            return TIMED_OUT;
        delay(50);
    }

    // We've had a reply!
    // Read the UDP header
    uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header
    // Check that it's a response from the right server and the right port
    if ( (iDNSServer != iUdp.remoteIP()) || 
        (iUdp.remotePort() != DNS_PORT) )
    {
        // It's not from who we expected
        return INVALID_SERVER;
    }

    // Read through the rest of the response
    if (iUdp.available() < DNS_HEADER_SIZE)
    {
        return TRUNCATED;
    }
    iUdp.read(header, DNS_HEADER_SIZE);

    uint16_t header_flags = htons(*((uint16_t*)&header[2]));
    // Check that it's a response to this request
    if ( ( iRequestId != (*((uint16_t*)&header[0])) ) ||
        ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) )
    {
        // Mark the entire packet as read
        iUdp.flush();
        return INVALID_RESPONSE;
    }
    // Check for any errors in the response (or in our request)
    // although we don't do anything to get round these
    if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) )
    {
        // Mark the entire packet as read
        iUdp.flush();
        return -5; //INVALID_RESPONSE;
    }

    // And make sure we've got (at least) one answer
    uint16_t answerCount = htons(*((uint16_t*)&header[6]));
    if (answerCount == 0 )
    {
        // Mark the entire packet as read
        iUdp.flush();
        return -6; //INVALID_RESPONSE;
    }

    // Skip over any questions
    for (uint16_t i =0; i < htons(*((uint16_t*)&header[4])); i++)
    {
        // Skip over the name
        uint8_t len;
        do
        {
            iUdp.read(&len, sizeof(len));
            if (len > 0)
            {
                // Don't need to actually read the data out for the string, just
                // advance ptr to beyond it
                while(len--)
                {
                    iUdp.read(); // we don't care about the returned byte
                }
            }
        } while (len != 0);

        // Now jump over the type and class
        for (int i =0; i < 4; i++)
        {
            iUdp.read(); // we don't care about the returned byte
        }
    }

    // Now we're up to the bit we're interested in, the answer
    // There might be more than one answer (although we'll just use the first
    // type A answer) and some authority and additional resource records but
    // we're going to ignore all of them.

    for (uint16_t i =0; i < answerCount; i++)
    {
        // Skip the name
        uint8_t len;
        do
        {
            iUdp.read(&len, sizeof(len));
            if ((len & LABEL_COMPRESSION_MASK) == 0)
            {
                // It's just a normal label
                if (len > 0)
                {
                    // And it's got a length
                    // Don't need to actually read the data out for the string,
                    // just advance ptr to beyond it
                    while(len--)
                    {
                        iUdp.read(); // we don't care about the returned byte
                    }
                }
            }
            else
            {
                // This is a pointer to a somewhere else in the message for the
                // rest of the name.  We don't care about the name, and RFC1035
                // says that a name is either a sequence of labels ended with a
                // 0 length octet or a pointer or a sequence of labels ending in
                // a pointer.  Either way, when we get here we're at the end of
                // the name
                // Skip over the pointer
                iUdp.read(); // we don't care about the returned byte
                // And set len so that we drop out of the name loop
                len = 0;
            }
        } while (len != 0);

        // Check the type and class
        uint16_t answerType;
        uint16_t answerClass;
        iUdp.read((uint8_t*)&answerType, sizeof(answerType));
        iUdp.read((uint8_t*)&answerClass, sizeof(answerClass));

        // Ignore the Time-To-Live as we don't do any caching
        for (int i =0; i < TTL_SIZE; i++)
        {
            iUdp.read(); // we don't care about the returned byte
        }

        // And read out the length of this answer
        // Don't need header_flags anymore, so we can reuse it here
        iUdp.read((uint8_t*)&header_flags, sizeof(header_flags));

        if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) )
        {
            if (htons(header_flags) != 4)
            {
                // It's a weird size
                // Mark the entire packet as read
                iUdp.flush();
                return -9;//INVALID_RESPONSE;
            }
            iUdp.read(aAddress.raw_address(), 4);
            return SUCCESS;
        }
        else
        {
            // This isn't an answer type we're after, move onto the next one
            for (uint16_t i =0; i < htons(header_flags); i++)
            {
                iUdp.read(); // we don't care about the returned byte
            }
        }
    }

    // Mark the entire packet as read
    iUdp.flush();

    // If we get here then we haven't found an answer
    return -10;//INVALID_RESPONSE;
}