HC-SR04 ultrasonic sensor - TCP Messaging

Dear all, i have a problem with TCP messaging with the HC-SR04 sensor.
When i use this code -


#include <NewPing.h>
#include <SPI.h>
#include <Ethernet.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 475 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress serverIP(***********************);
int serverPort=
;

EthernetClient client;

void setup() {
Serial.begin(115200);

void loop() {
delay(2000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
client.print("Ping: ");
client.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
client.println("cm");
}


This code works with the serial monitor and shows the right reading of the sensor, but it does not tcp message because i havent added this bit of code to the setup.

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");

for(;
;
}

delay(1000);
Serial.println("connecting to Server ...");

if (client.connect(serverIP, serverPort)) {
Serial.println("connected");
}
else {

Serial.println("connection failed");
}
}


but as soon as i add this part of the coding the tcp messaging starts to work but it only prints 0cm on the serial monitor and on my server.jar for tcp.

but if i take that bit of code out it wont tcp message but it will print the right CM readings on the serial monitor.

i cant seem to work out why it does it?

Would be great to get some feedback

kind regards

Lewis J

What happens if you change the delay(2000); to delay(40);?