[Solved] Ethernet shield doesn't connect as client.

Hi everyone,

I have been working with Arduino Uno and Ethernet Shield W5100 for some days and I can't use the board as client making HTTP requests.

I am working on a Windows 7 machine, port 80 is open, and have connected the Ethernet shield directly to the pc (no router no available to me).I have tried the WebServer example and it works fine so I would assume that the connection between the pc and Arduino is ok.
In addition the connection is stabilized with a static IP address, since DHCP server is not enabled on my pc and I can't enable it since I am not the admin, but working with IP static address is fine at the moment.

However unfortunately the Web Client example doesn't work. I have tried to debug and change the code to detect where the connection fails and I have that client.connect returns 0.

Here my simplified code to just check if the connection can be stabilize :

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


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
IPAddress ip(192, 168, 1, 9);

EthernetClient client;

void setup() {
  
  Serial.begin(9600);
  delay(1000);
  Ethernet.begin(mac, ip);
  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());  //Get the right IP

  Serial.println("connecting...");
  delay(10000);  // Seen that sometimes there could be a delay issue so added a rather large delay here
  
  if (client.connect(server,80)){
    Serial.println("OK");
  }
  else {
    Serial.println("NO");
  }

}

void loop()
{

}

Result is always NO in Serial monitor.
Has anyone idea about what is going wrong? Maybe I should do something with the connection between pc and device but WebServer example works ok so no idea about what.

I also tried the same example with my personal pc, direct connection Arduino - MacAir which is connected to the Internet over Wi-Fi and firewall is off , result is the same WebServer is ok Webclient no.

Thanks in advance for you help :wink:

Do you have Internet Connection Sharing (ICS) set up on the Windows box?

Yes , but unfortunately nothing. Port 80 is open for incoming and outgoing connection as well.

The Windows PC ethernet interface is 192.168.1.1?

It's 192.168.1.2 .

Since the WebServer example works with that address I would assume it is correct or is there anything i am missing about it?

Sorry if i say it only now but thank you for helping (even if no results yet :slight_smile: )

Then the ethernet shield is using the wrong IP for the gateway (default is 192.168.1.1). You have a choice.

  1. Change the PC IP to 192.168.1.1
    or
  2. Set the gateway on the ethernet shield to 192.168.1.2

Solved. I enabled again the ICS on the Windows box and now all is working fine.