Ethernet client unreliable, connection fails to apache sometime

Hi, I'm tried to connect to apache on my Win10 notebook. I want to catch the php in apache. But the question is it doesn't connect success everytime. Also, I tried to connect to Google. It doesn't connect everytime too, but the connect success times is much better than apache.

Here is the version :

apache 2.4.41
arduino 1.8.12
MEGA 2560
Enternet W5100

Here is the code

#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>               
#include <hd44780.h>                   
#include <hd44780ioClass/hd44780_I2Cexp.h>  
#include <avr/pgmspace.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 10, 3 };
byte gateway[] = { 192, 168, 10, 254 }; // internet access via router 
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask   
byte server[] = { 192, 168, 10, 2    }; //64, 233, 187, 99 
//char server[] = "www.google.com";

EthernetClient client;
hd44780_I2Cexp lcd;  //宣告lcd


void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(115200);
  
  lcd.init(); // initialize the lcd 
  lcd.noBacklight(); 
  delay(300);
  lcd.backlight(); //開啟背光
  
  Serial.println("connecting...");

  if (client.connect(server, 80)) {   
    Serial.println("connected");
    client.println("GET /php2arduino/test.php HTTP/1.0");
    client.println();
       
  } else {
    Serial.println("connection failed");
  }
 
       
}

void loop()
{
  lcd.setCursor(0, 0); 
  if (client.available()) {   
    
    String d= client.readStringUntil('\n');
    Serial.print (d+'\n');
    lcd.print(d);
    delay(1000);
    lcd.init();
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
  
}

Here is the result to connect apache. Try 4 times, only first connect success .

connecting...
connected
HTTP/1.1 200 OK
Date: Fri, 24 Apr 2020 07:08:39 GMT
Server: Apache/2.4.41 (Win64) PHP/7.3.12
X-Powered-By: PHP/7.3.12
Content-Length: 3
Connection: close
Content-Type: text/html; charset=UTF-8

UFO

disconnecting.



connecting...
connection failed

disconnecting.



connecting...
connection failed

disconnecting.



connecting...
connection failed

disconnecting.

I have look for the past topic, some said it cause by memory problem that I should avoid use string.
But I can't use F() on GET the HTTP. Or maybe there has a way to replace readStringUntil ?
I'm a beginner, so hope get some example help.

Thank for your kind! :slight_smile:

I'd start with putting the I2C stuff in a drawer and locking it, until the connection happens more reliably.

Since you are running Apache on your laptop, you can look at the log files. Perhaps the problem isn't with the Arduino at all.