Ethernet shield, client.connect(server, port) only works with URL

Hello.

I'm new to this forum since most of the time the examples en forum discussions were enough to solve my problems with Arduino.

In this topic

https://forum.arduino.cc/index.php?topic=168374.0

TM describes his problem of connecting to a client where the IP connection works fine, the URL connection does not.

In my set up this is the other way around. The URL works fine, IP does not. I've tried all kinds of things, with existing websites.

Also, different ethernet libs have different results. With the 2.0.0. the URL doesn't work either.. Currently using 1.1.2.

#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x40, 0xF3 };//mac address on board sticker

// 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(192, 168, 0, 40);  // numeric IP for Google (no DNS), NOK
//IPAddress server(74, 125, 232, 128);  // numeric IP for Google (no DNS), NOK
//IPAddress server(216, 58, 212, 142);  // numeric IP for Google (no DNS), NOK
//IPAddress server(139, 130, 4, 5);  // telstra australia, NOK
//byte server[] = { 74,125,232,128 }; // Google, NOK
char server[] = "www.google.com";    // name address for Google (using DNS), Ok
//char server[] = "ns1.telstra.net"; // Ok

EthernetClient client;

void setup() {

Serial.begin(9600);

  Serial.print("check IP");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    //Serial.println(client.remoteIP());
    // 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");
  }
  beginMicros = micros();
}

Help would really be appreciated..

URL will never work in connect. but you mean hostname.

Host: header is mandatory for HTTP/1.1 and necessary if you connect with IP to a server with more hostnames. connecting to google with IP only can't work

What happened to Ethernet.begin()? If your router doesn't resolve domain names, you will have a problem with dns.

Thanks for the replies.

I solved the problem by adding the gateway to the EthernetClient, like this:

EthernetClient client; //PC, localhost
byte ipLocalWebServerOnPc[] = { 192, 168, 0, 35 };
byte gateway[] = { 192, 168, 0, 1 };

Next issue is to really understand why this works. ;D Networking is a field of knowledge where some work needs to be done...

gateway address is used for dns address if that is not supplied.