user29179: I'm using an Arduino Uno and Ethernet shield with the WIZnet chip W5100. The WebServer code example is working. DHCP is working, the Ethernet shield is receiving an IP address 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 an IP address manually, not using DHCP. I tried making pin 4 of the SD memory card SPI to output pinMode(4, OUTPUT); digitalWrite(4, HIGH); But it doesn't work. I get on my Serial line connection failed or it returns "connected" then "Disconnecting..." immediately after because client.connect has returned -1.
The example code I'm using:
#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 };
// 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 (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    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);
  }
}

This is my output:

Using a local server IP address like my router's IP address, the connection works. But the issue is mainly in the WAN. Any help would be appreciated.

user614: This doesn't look right:
if (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    ...

If the first call to client.connect() succeeds, you call it again immediately, and the error message you're seeing comes from that second call. This really is not surprising — I can't imagine a scenario in which that would actually work.
What you really want is something more like:
if (result = client.connect(server, 80)) {
    Serial.print(result);
    ...


29179: <p>I'm using an Arduino Uno and Ethernet shield with the WIZnet chip W5100. The WebServer code example is working. DHCP is working, the Ethernet shield is receiving an IP address from the Linksys modem "router" and everything is fine.</p>

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

<p>I tried connecting to different websites. I tried assigning an IP address manually, not using DHCP. I tried making pin 4 of the SD memory card SPI to output <code>pinMode(4, OUTPUT); digitalWrite(4, HIGH);</code> But it doesn't work. I get on my Serial line connection failed or it returns "connected" then "Disconnecting..." immediately after because <code>client.connect</code> has returned -1.</p>

<p>The example code I'm using:</p>

<pre><code>#include &lt;SPI.h&gt;
#include &lt;Ethernet.h&gt;

// 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 };
// 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 (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    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);
  }
}
</code></pre>

<p>This is my output:</p>

<p><a href="https://i.stack.imgur.com/fDXGC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fDXGC.png" alt="enter image description here"></a></p>

<p>Using a local server IP address like my router's IP address, the connection works. But the issue is mainly in the WAN. Any help would be appreciated.</p>
 614: <p>This doesn't look right:</p>

<pre><code>if (client.connect(server, 80)) {
    Serial.print(client.connect(server,80));
    ...
</code></pre>

<p>If the first call to <code>client.connect()</code> succeeds, you call it again immediately, and the error message you're seeing comes from that second call. This really is not surprising &mdash; I can't imagine a scenario in which that would actually work.</p>

<p>What you really want is something more like:</p>

<pre><code>if (result = client.connect(server, 80)) {
    Serial.print(result);
    ...
</code></pre>
