Ethernet client connect can return negative number

I spent a good half a day today trying to figure out why my ethernet client code suddenly does not work anymore.

It turns out if you are using "hostname" to connect, ethernet client will do a dns lookup, and if dns lookup fails, connect returns a negative value, which tests to TRUE if you do this code

if (client.connect("www.google.com",80)) {
  //apparently connected, NOT if there was a dns error.
}

The correct code is to explicity test for 1

if (client.connect("www.google.com",80)==1) {
}

I think someone should change the WebClient example that comes with Arduino Ethernet library and update the documentation as well.

If you want to evaluate why it failed, here are the return value meanings:

#define SUCCESS          1
#define TIMED_OUT        -1
#define INVALID_SERVER   -2
#define TRUNCATED        -3
#define INVALID_RESPONSE -4

edit: I changed my playground web client examples to evaluate to success if return value is 1 rather than not 0.

After a test and then evaluating the dns library code, the "INVALID RESPONSE" is a return value value from -4 to -10. I tried connecting to a non-existent domain and the client.connect() function returned -5.

@doughboy: Did you want to report the error? Here is a link to the github for Arduino. That is where we are supposed to report website, example, and reference errors.

thanks. I just entered a new report.

I added the code from dns.cpp as a comment to your report to show the possible return values.

Update: The reference return value was changed to reflect the correct return values, but the example on that page is still incorrect. I reported that error to Scott Fitzgerald, the person who did the change.