Pages: [1]   Go Down
Author Topic: server.available() problem  (Read 913 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

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

Code:
#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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

> 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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8976
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Pages: [1]   Go Up
Jump to: