WebServer, WebClient and DNS... problem. Zoomkat example.

Hello,
arduino 2009 with official eth shield and IDE 1.0 here :slight_smile:
Someday ago, zoomkat saves my day with an example to mix web server and web client.
Now I have implemented a dns with openDNS... but the thing goes strange.

First point.
If there is a internet connection, everything works fine.
My address is actually resolved and the connection (sending data to pachube) works.

But there are some strange behaviors when there is NOT an internet connection.
In particular:

 if (client.connect(myserver, 80)) {

always returns... true. Even if there is no internet connection.
Even if I unplug the eth cable from the Ethernet shield.

How's that? Any suggestion?
When there is no internet connection the packets sniffing with whireshark returns a dns query and an ICMP host unreachable.
But I don't know why the if always return true.

If I use IPAddress myserver instead of name, removing dns code, the if works fine.

Any help will be appreciated.
It's the last piece of my project, I can live without that but... I prefer having everything tuned fine!

This is my code:

/************ ETHERNET STUFF ************/
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,9); // IP in lan
IPAddress gateway(192,168,0,1); // Internet access via router
IPAddress subnet(255,255,255,0); // Subnet mask
IPAddress dns(208,67,222,222); // OpenDNS IP
EthernetServer server(84); // Porta webserver

/*--------- Pachube ---------*/
// IPAddress myserver(216,52,233,122); //  The address of the server you want to connect to (pachube.com)
char myserver[] = "pachube.com"; 
EthernetClient client;  // initialize the library instance

setup:

  Ethernet.begin(mac,ip,dns,gateway,subnet);
  server.begin();

loop.
Here's come the strangeness.

    if (client.connect(myserver, 80)) {
      pachubestatus=1;
      PgmPrintln("Connecting to Pachube...");
      // send the HTTP PUT request. 
      client.print(F("PUT /v2/feeds/xxx.csv HTTP/1.1\n"));
      client.print(F("Host: api.pachube.com\n"));
      // send the Pachube API key here:
      client.print(F("X-PachubeApiKey: xxx\n"));
      client.print(F("Content-Length: "));
      client.print( lngstring ); 
      client.print(F("\n"));
      client.print(F("Connection: close\n"));
      client.println();

      // here's the actual content of the PUT request:
      client.print(F("t_out,"));
      client.print(charbuffertout);// t_out
      
      PgmPrintln("Connection Done... Maybe.");
    } 
    else {
      // if you couldn't make a connection:
      pachubestatus=0;
      PgmPrintln("Connection failed!");
    }
    client.stop();
    delay (500);

Thankyou

PgmPrintln("Connection Done... Maybe.");

I really enjoy a "definite maybe"! XD

When I was an air traffic controller, a controller friend of mine used to say "I'd rather be lucky than good". Makes you want to rush out and buy an airline ticket, doesn't it? lol

edit: My apology. I didn't mean to be a troll.

SurferTim:
edit: My apology. I didn't mean to be a troll.

You don't seem one to me, you have nothing to apologize :slight_smile:
I noticed that my code strictly follows my mood... when I'm happy my arduino code is quite funny too :smiley:

well I have actually implemented a solution:

    if (client.connect(myserver, 80) && client.connected() ) {

This returns properly in every condition.

My code seems done but I'd like to understand why plain client.connect doesn't works and why I have to add a control on client.connected.
If someone can point me out I'll really appreciate :slight_smile:

BTW this seems a better solution at all.
Plain client.connect with IPAddress myserver hangs for about 30 second when there is no internet connection.
But client.connect && client.connected seem to hang within 15 second when there is no internet connection.

Do you get the same results if you use the ip for the server? Or is it always returning true just when you use the dns?

SurferTim:
Do you get the same results if you use the ip for the server? Or is it always returning true just when you use the dns?

No. If i use the ip for the server with IPAddress... everything works fine in every condition.
The thing goes odd only when I use dns.

Btw I made a test sketch.
This sketch returns true in every condition, for me at least.
Note:
If i start up the arduino with no internet connection, the sketch works fine.
If I switch on the internet connection and then turn it off... the sketch keep returning true.
If somebody wanna test it... here it is:

// sketch test dns

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

/************ ETHERNET STUFF ************/
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,9); // IP in lan
IPAddress gateway(192,168,0,1); // Internet access via router
IPAddress subnet(255,255,255,0); // Subnet mask
IPAddress dns(208,67,222,222); // OpenDNS IP

/*--------- Pachube ---------*/
// IPAddress myserver(216,52,233,122); //  The address of the server you want to connect to (pachube.com)
char myserver[] = "pachube.com"; 
EthernetClient client;  // initialize the library instance

void setup(){
  Serial.begin(115200); 
  Serial.println(" Test DNS ");
  Ethernet.begin(mac,ip,dns,gateway,subnet);
}

void loop(){

  if (client.connect(myserver, 80)) {
    Serial.println("Connecting to Pachube...");
    Serial.println("Connection Done... Maybe.");
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("Connection failed!");
  }
  client.stop();
  delay (1000);

}