Ethernet client.read(); seems to block

Hello,
I’m having a problem with my Arduino Ethernet Server, it works OK when it’s constantly receiving data from a client but as soon as the client stops sending regularly interval data the Arduino seems to hang on client.read();

I’m using an Arduino Micro and a Wiznet 5100 Ethernet PCB connected over SPI and using the standard Ethernet.h library.

My client app is a c# app and sends 90/90/90/90\n when values change. Previously I sent this data on a fixed timer and the server was much happier. However I want to limit the data and that’s the reason for the change.

#include <Servo.h>
#include <Ethernet.h>

Servo S1, S2, S3, S4;     // create servo objects to control servo's

byte mac[] =
{
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

EthernetServer server(10100);// telnet defaults to port 10100
EthernetClient client;

int Pos1 = 90, Pos2 = 90, Pos3 = 90, Pos4 = 90;

unsigned long previousTime = 0; // last time update
long interval = 1000; // interval at which to do something (milliseconds)
String message = "";

void setup()
{
  Serial.begin(57600);    // initialize serial communication at 9600 bits per second:
  
  message.reserve(128);
  
  pinMode(10,OUTPUT);
  
  S1.attach(3), S2.attach(5), S3.attach(6), S4.attach(9);  // attaches the servo's to servo object's
  
  if (Ethernet.begin(mac) == 0)
  {
    Ethernet.begin(mac, ip, myDns, gateway, subnet);   // initialize the Ethernet device not using DHCP:
  } 
}

void loop()
{
  client = server.available();  // wait for a new client:
  if(client)
  {
    while(client.connected())
    {
      if (client.available())
      {
        message = receiver();  //read incoming bytes from client    90/90/90/90\n over and over
        
        char parseChars[16];   // an array to store the received data
        message.toCharArray(parseChars, 32);
        sscanf(parseChars,"%d/%d/%d/%d", &Pos1, &Pos2, &Pos3, &Pos4);
        
          S1.write(Pos1);                  // sets the servo position according to the scaled value
          S2.write(Pos2);                  // sets the servo position according to the scaled value
          S3.write(Pos3);                  // sets the servo position according to the scaled value
          S4.write(Pos4);                  // sets the servo position according to the scaled value

        sender();  //heartbeat to client
       }
     }
     
     //Serial.println("Dropped Client");                      /////////////////////////////////////////////////
     S1.write(90);                  // sets the servo position to zero after drop
     S2.write(90);                  // sets the servo position to zero after drop
     S3.write(90);                  // sets the servo position to zero after drop
     S4.write(90);                  // sets the servo position to zero after drop
  }
}

void sender()
{
  unsigned long currentTime = millis();
  if(currentTime - previousTime > interval)
  {
    previousTime = currentTime;  
    client.write("pb");
  }
}

String receiver()
{
  static char receivedChars[64];
  static byte ndx = 0;

 while(true)
  {
    char rc = client.read();
    if (rc != '\n')
    {  
      receivedChars[ndx] = rc;
      ndx++;
    }
    else
    {
      ndx = 0;
      return receivedChars;
    }
  }
}

While the client is still connected and no data is received, nothing much will happen, it isn't doing client.read(), it's just sitting there looping around the while(client.connected()) loop and not executing anything inside the one and only if statement in that loop.

Thanks for the help. An if (!client.available()){} helped get me on my way.

Thank you

You can also use 'else':

if (client.available())
{
  message = receiver();
  //etc...
)
else //This is the equivalent of: if (!client.available())
(
  //do the other stuff.
}

It's worth using because if you change the first if statement, the else part will still hold true, otherwise you probably have to edit the other if statement. It's also easier to read, especially when more complicated if statements are used.