Arduino using ethernet sheild WebClient example code is not working

I’m using Arduino UNO and ethernet shield with the WIZnet chip W5100. The WebServe code example is working the DHCP is working the ethernet shield is receiving an ip from the linkSys modem “router” and everything is fine.

But i cannot connect to a web site using the WebClient code. I found out that client.connect(server, 80); is returning -1 “Time_OUT” and it is supposed to return 1 according to the Arduino Ethernet library.

I tried connecting to different websites. I tried assigning ip address manually not using the DHCP. I tried making pin 4 of the SD memory card SPI to output pinMode(4, OUTPUT); digitalWrite(4, HIGH);
But useless. I get on my Serial line connection failed or it returns “connected” then “Disconnecting…” immediately after because the client.connect has returned -1.

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x28, 0xAD, 0xBE, 0xEF, 0xB9, 0xED };
int result;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:

  Serial.println("connecting...");
  delay(2000);
  Serial.println(Ethernet.localIP());

  // if you get a connection, report back via serial:
  if (result = client.connect(server, 80)) {
    Serial.print(result);
    Serial.println();
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

I tried using the server ip directly but “Connection Failed”
This is my output:

Any help would be appreciated.
PS: This code worked on other networks but not on my home network with the linkSys router. I checked also if i have any kind of security implemented on the modem but there was nothing.

Normally a return value less than zero (negative) indicates a DNS fail.

Ok then using the web address is not resolved so it is a DNS fail. I tried also using the IP address directly i gave me connection failed. Means client.connect() returned null. So i really don't know what to try else. :frowning:

If you used that IP in your code, that will timeout. Try this IP for Google instead. 216.58.194.68

Yes i tried this ip it too... Tried other websites too. There is something wrong. Maybe the DSL modem itself. I checked if there are any kinds of firewalls or securities implemented but there was nothing.

Are you using the IPAddress for the server IP?

 IPAddress server(216,58,194,68);  // numeric IP for Google (no DNS)

Yes i tried using this google ip but it didn't work. Connection Failed. :confused: I dont think it is a DNS issue.

Thanks SurferTim for the help. The issue was in the mac address. I had to use the ordinary mac address of the example code.

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

Not sure if this is relevant to the present discussion, but after quite a few hours of frustration with similar problems I discovered:

(a) my Ethernet shield is an Ethernet shield 2

(b) the Ethernet chip on my board is a Wiznet W5500, not a 5100

(c) version 1.6.x of the IDE doesn't appear to support the 5500

(d) version 1.7.11 does support the 5500

(e) version 1.7.11 comes from arduino.ORG, not arduino.CC

(f) some Example programs that come with 1.7.11 have #include Ethernet.h which must be changed to #include Ethernet2.h (I know on Arduino.cc under Libraries there is a link to Ethernet2, but it appears to be broken.)

I hope the above will help anyone else who is pulling out the hair. I now have chatServer and webServer examples running and can press on to something useful.

David