connection failed!

Hi again,

Thank you all for your help.
I started my Ethernet shield and ran some of primary sketches. Now I want to test advanced programs which I will need your experience. The code I tested is from Arduino examples:

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


byte mac[] =   { 0x00, 0x06, 0x4F, 0x0D, 0xAC, 0xA9 };
byte ip[] = { 192,168,1,5 };
byte gateway1[] = { 2,180,117,1} ;
byte dns1[] = { 255 , 255 , 255 , 0 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip,dns1,gateway1);
  Serial.begin(9600);
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

what i get in serial monitor is:
connecting...
connection failed

disconnecting.


Do I need to change any settings such as firewall, router configuration, etc?
Thanks :)

This is the old style declarations, but it should work if you get all the numbers correct. If your localnet is 192.168.1.x/24, and you use the correct ip for Google, then

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

byte mac[] =   { 0x00, 0x06, 0x4F, 0x0D, 0xAC, 0xA9 };
byte ip[] = { 192,168,1,5 };
byte gateway1[] = { 192,168,1,1} ;
byte netmask[] = { 255 , 255 , 255 , 0 };
byte server[] = { 173, 194, 46, 16 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip, gateway1, gateway1, netmask);
  Serial.begin(9600);
// rest of your code

SurferTim you are great! It worked! Thank you :) what is gateway? I type 192.168.1.1 in ,my address bar and get the following information: LAN IPv4 Address: 192.168.1.1 MAC Address: 00-26-5a-de-33-50 Default Gateway: 2.180.117.1

Is my gateway 192.168.1.1 or 2.180.117.1 ? why is DNS the same as gateway in your code?

I think the 2.180.117.1 is the IP address of your router in the WAN network. 192.168.1.1 is the IP address of your router in the LAN network. A gateway such as your router has several IP addresses, one for each network. It holds the key to several networks. For your arduino ethernet adapter, it is not a router so has only one address 192.168.1.5 and to access the WAN or internet, it needs to know which IP address on your LAN has access to the outside, so ITS gateway is 192.168.1.1

BTW, does your router allow 192.68.1.5 to be taken by your ethernet shield? Most routers reserve this address so they can assign to connected devices.

If you replace this line

Ethernet.begin(mac, ip,dns1,gateway1);

with this line

Ethernet.begin(mac);

your router should assign an IP address such 192.168.1.102, etc, if it has DHCP enabled.

Using Windows “Start > Run > cmd > ping”, I was able to ping 2.180.117.1, and it was a long way away, 418 msec to reply, but —> accessible from outside <—. I wonder what that is? When I ping 192.168.1.1 [my local router here], it takes only 1 msec. And ping google.com only takes only 19 msec.

Why is this line in the original code? It must be somebody’s primary server address. ???

byte gateway1[] = { 2,180,117,1} ;

oric_dan: Why is this line in the original code? It must be somebody's primary server address. ???

byte gateway1[] = { 2,180,117,1} ;

Good eye!

I inserted this:

Ethernet.begin(mac);

but still got 192.168.1.5

It works then.

oric_dan: It works then.

Yep, only this time the address is assigned to your shield instead. So are you still experiencing any connection problems?