Ethernet 2.0.2 and flush()

gonzales

Well-known member
Hi!
I found an interesting feature of the library Ethernet.
I use it to organize the server on Teensy with W5500, so i'm doing everything as stated in the documentation. (https://docs.arduino.cc/libraries/ethernet/#Server Class)
CSS:
void loop() {
  // check for any new client connecting, and say hello (before any incoming data)
  EthernetClient newClient = server.accept();
  if (newClient) {
    for (byte i = 0; i < 8; i++) {
      if (!clients[i]) {
        newClient.print("Hello, client number: ");
        newClient.println(i);
        // Once we "accept", the client is no longer tracked by EthernetServer
        // so we must store it into our list of clients
        clients[i] = newClient;
        break;
      }
    }
  }

  // check for incoming data from all clients
  for (byte i = 0; i < 8; i++) {
    while (clients[i] && clients[i].available() > 0) {
      // read incoming data from the client
      Serial.write(clients[i].read());
    }
  }

  // stop any clients which disconnect
  for (byte i = 0; i < 8; i++) {
    if (clients[i] && !clients[i].connected()) {
      clients[i].stop();
    }
  }
}
For my tasks, the client remains connected to the server until it disconnects itself. I use write and flush to transfer data to the client.
Code:
if (clients[i]) {
  if (clients[i].connected()) {
    clients[i].write(buff, sizeof(buff));
    clients[i].flush();
  }
}

This work fine, but sometimes I noticed that the server was rebooted (Wathdog) during sending. It took me a long time to figure out what was going on, but I found out that the problem is in the function flush(). If the client has lost connection with the server, stop() is not called, so for the server, the client is still alive and connected, but there is an infinite loop in the function flush(), from which there is no way out.
Code:
void EthernetClient::flush()
{
    while (_sockindex < MAX_SOCK_NUM) {
        uint8_t stat = Ethernet.socketStatus(_sockindex);
        if (stat != SnSR::ESTABLISHED && stat != SnSR::CLOSE_WAIT) return;
        if (Ethernet.socketSendAvailable(_sockindex) >= W5100.SSIZE) return;
    }
}

I'm going to make an exit from flush() by timeout with stopping client.
Does anyone have any other ideas?
 
Is this a Teensy 4 or 3 or…?

I tested the QNEthernet library to work with the W5500 chip, if you’d like to give that a try instead of the “Ethernet” library. You’ll need to change the driver in qnethernet_opts.h by uncommenting the QNETHERNET_DRIVER_W5500 line.
 
It is T4.1.
I use QNEthernet for Native Ethernet with T4.1, but I didn't know that it could be used for W5500. I will try!
Thanks
 
Which headers are you including? I’m asking because your capitalization of “Native Ethernet” suggests to me that you’re also trying to use the NativeEthernet library, and your mention of the Ethernet library suggests you’re trying to use that as well. But I wanted to clarify.
 
Last edited:
Is this a Teensy 4 or 3 or…?

I tested the QNEthernet library to work with the W5500 chip, if you’d like to give that a try instead of the “Ethernet” library. You’ll need to change the driver in qnethernet_opts.h by uncommenting the QNETHERNET_DRIVER_W5500 line.

I have tested QNEthernet for my tasks, it work fine. Thank one more time!
 
Back
Top