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)
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.
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.
I'm going to make an exit from flush() by timeout with stopping client.
Does anyone have any other ideas?
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();
}
}
}
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?