Slow connection to local server but fast to remote using ESP8266 WIFI

I am using a NodeMCU with the Arduino IDE. When I run the sketch below to connect to adafruit it takes about 50ms. When I run it to connect to my local server, XAMPP on my PC, it takes 1000ms. When I connect to the local server from my other PC it loads almost instantaneously so it’s not the local server that is slow. Does anyone have any idea why this might be?

/*
 *  Simple HTTP get webclient test
 */
 
#include <ESP8266WiFi.h>
 
const char* ssid     = "virginmedia545641";
const char* password = "mypassword";
 
const char* host = "192.168.0.10";
//const char* host = "www.adafruit.com";

String url = "/electronics/test.php";
//String url = "/testwifi/index.html";
const int httpPort = 80;
String remoteData;

 
void setup() {
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  unsigned long testStartTime = millis();
  
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(1);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    remoteData = client.readStringUntil('\r');
  }
  Serial.println(millis() - testStartTime);
  //Serial.println(remoteData);
  //delay(3000);
}
const char* host = "192.168.0.10";

A host name and an IP address are not the same thing.

The syntax for client.connect is:

client.connect(ip, port)
client.connect(URL, port)

I tried using a 4 byte array for the ip and it connected with the same slow speed as using the string ip address.

You are lucky to have a connection at all! I have a similar setup but I cannot connect locally at all. Connection works from the ESP8266 to the internet as well as to my router. Other devices on the local net have no problem to connect to a local server. I wonder whether it may have the same cause.

I am using a NodeMCU programmed with the PlatformIO IDE on a mac. The code is taken from https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino. My router is a Fritz!Box 7390

I have exactly the same problem. I am using NodeMCU (ESP-12E Module) with Arduino IDE. The program uploads and can connect to any server except one on the local subnet. I was trying to drive data to a NodeJs server running on a local machine and the client.connect failed every time.

To confirm the problem was with the local address I set up a route through DynDns which directed back to my Internet connection and port forwarded the incoming request to my local server. As soon as I did that it worked perfectly.

It looks like the library is directing all requests to the router's default route instead of going directly to the device on the local subnet.

Old posting, I know. No solutions here. But I have to add my experience: by peppering my code with "serial.print millis();" statements i have found that "client.connect" is where the big delay comes from. In my case it can be anywhere from 4 to 8 seconds to respond. When I enter the same address in my browser's url bar, the response is nearly instantaneous.