failded to connect to server

hello
i’m working on a project that sends http GET request to a server,
however when it failed to connect for the first time, instead get connection in the next turn it cant connect.
I’m using the Ethernet library.
anyone has that problem before?

thanks.

anyone has that problem before?

Lots of people. The problem is almost always in the code.

Basic client test code you can try unmodified to see if you can get a a reply from the server.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Hi, thank you for the quick response.
My problem is that i have a connection , my program based on your example.
but when it's not able to connect , it's print "connection failed" , instead of doing re-connection in the next turn, it just doesn't connect and prints "connection failed" until i restart the sketch

noris:
Hi, thank you for the quick response.
My problem is that i have a connection , my program based on your example.
but when it's not able to connect , it's print "connection failed" , instead of doing re-connection in the next turn, it just doesn't connect and prints "connection failed" until i restart the sketch

zoomkat DID say "Basic client test code..."

HI PaulS
I know, but the principal is the same, send HTTP GET request every loop turn.
My problem is that it work fine but when it failed to connect, it cant establish new connection after it.
If you can recommend me any examples that repeated sends HTTP request , or things i might missed , i'll be grateful.

If you can recommend me any examples that repeated sends HTTP request , or things i might missed , i'll be grateful.

SurferTim has code on the playground that handles just about everything that could possibly fail.

I can't find it right now. Hopefully, he'll be along with a link.

I think this is what you need. It has all the latest error checking and fault tolerance I can think of.
http://playground.arduino.cc/Code/WebClient

SurferTim:
I think this is what you need. It has all the latest error checking and fault tolerance I can think of.
http://playground.arduino.cc/Code/WebClient

Bookmarked!