With arduino mega 2560 + ethernet shield + arduino 0022 + standard library (#include <SD.h>, #include <Client.h>, #include <Ethernet.h>, #include <Server.h>, #include <Udp.h>, #include <SPI.h>)
I have make this sketch (symbolic)(real code take ~2000 lines):
LOOP
every 200ms -> read bouton + say launch task
if task 1 : do task 1 part 1 (if end next step part 2) or task 1 part 2 (ect...) or ....
if task 2 : ......
if ....
if ethernet do task ethernet : send a file on SD
END LOOP
It's work but i must have each loop time less time than 200ms or not so far away. So as your see, i have cut every task on pieces, but ethernet was in one piece and if i ask for big file (10ko!!) on SD cart, it take 2-5 second, it's too big, i must have to make other task in //, i try to make ethernet in piece but compilator always say error like :
error: 'client' was not declared in this scope
when I try to compile this (symbolic) :
LOOP
every 200ms -> read bouton + say launch task
if task 1 : do task 1 part 1 (if end next step part 2) or task 1 part 2 (ect...) or ....
if task 2 : ......
if ....
if "!ethernet" do find client connection (if client ask 'client read' on next loop)
if "client read" do read 1 byte and analyse data (if header end 'header end' on next loop)
if "header end" do read and send 1 line of file (if file all send 'end of client' on next loop)
if "end of client" do end of client and validate '!ethernet' on next loop
END LOOP
You are declaring your client within your first if() statement. It'll only exist for the life of that statement, so any access outside that statement is invalid. You must declare it somewhere that remains in scope for the entire time it is being used.
Yes, it was difficult to find how to declare client, but i find :
"Client client(4);" out of loop declare a global Client class for server mode, and it work fine : ie you can use client every where in the sketch, i can run my 200ms staff and cut the ethernet request even if the ethernet task have 5s time run.
Note : the line "Client client=server.available();" become "client=server.available();" in the code.