Go Down

Topic: server.available() problem (Read 941 times) previous topic - next topic

afasias

Hi there,

I have this code which is nothing more than the usual for a web server:

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

#include "Model.h"
#include "Controller.h"

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x33, 0x89 };
IPAddress ip(10,0,0,177);
EthernetServer server(80);

void setup() {
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac,ip);
  server.begin();
}

void loop() {
  if (EthernetClient client = server.available()) {
    // do stuf with client here.
  }
}


and I just discovered that: the server.available() call does not return "true" before the connected client sends its first byte.

Any idea why is this happening?

johnwasser

> the server.available() call does not return "true" before the connected client sends its first byte.
>
> Any idea why is this happening?

Yes.  server.available() is defined (http://arduino.cc/en/Reference/ServerAvailable) as returning a client THAT HAS DATA READY TO READ.  Fortunately for a web server the client sends an HTTP request before the server is expected to reply.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

afasias

Thank you for the reply.

The problem is that I want to implement a time-out. I want to cut off the client if it takes too long to complete the http request. But this way, I cannot start counting until the client sends the first byte. That means, that if the client never sends anything, it may happen that the server process will hang waiting for ever.

Also any timer/signal will not do any good because again the problem is that I cannot record the _moment_ that the connection was established.

Any ideas welcome...

johnwasser

You could make a custom version of the library that returns a connected client even if the client has no data available.  You probably don't want to always return the FIRST such client because you would then never get to the second client.  You should keep track of the last client returned and start with the client after that.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

afasias

Thanks, that's a good idea. I guess one could also subclass the EthernetServer and override the available method (although in general this is not a good practice).

Go Up