When connecting, client.connect() returns 0 I'm using an arduino UNO with ethernet shield. When following a few guides online for a school project I came across this issue. First code sample:


    #include &lt;SPI.h&gt;
    #include &lt;Ethernet.h&gt;

    EthernetClient client;

    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192, 168, 1, 25);
    byte server[] = { 64, 233, 187, 99 }; // Google

    void setup() {  
      Ethernet.begin(mac, ip);
      Serial.begin(9600);
      delay(1000);
      Serial.println(Ethernet.localIP());
    }

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


This doesn't work unfortunately. We successfully connect to our network, but client.connect() doesn't work and returns 0. Expected result:

SUCCESS: 1
TIMED_OUT: -1
INVALID_SERVER: -2
TRUNCATED: -3
INVALID_RESPONSE: -4

According to the documentation it shouldn't be able to return this. Why wouldn't this work? Thanks in advance.
You need to provide more than just the IP address. The Arduino also needs to know the default router address in order to send packets to the right place.

Unfortunately there isn't a way of giving it that information without also giving it a DNS address, so you have to do that as well.

You need to have:

IPAddress gateway(192, 168, 1, 1);
IPAddress dns(8, 8, 8, 8);
IPAddress ip(192, 168, 1, 25);


And then in setup:

Ethernet.begin(mac, ip, dns, gateway);


Alternatively you can rely on DHCP to give you all the details, and just use:

Ethernet.begin(mac);