I am trying to transmit an integer(distance reading) from one arduino to another over ethernet. I am using the wiznet library.
The first arduino is set up as a client and polls the other arduino (set up as a server) for a reading from an ultrasonic distance sensor.
My problem is writing the best code for the server as I don't fully understand the server operation. (I find it confusing that there are client instances as well as the expected server instances in the server code).
void CheckRequest()
{
SS1.println("CheckRequest");
char ClientData[BUFFERLENGTH] = {'\0'};
EthernetClient client = server.available();
if (client)
{
RequestFailureCounter = 0;
unsigned short int i = 0;
while (client.available() && i < BUFFERLENGTH)
{
ClientData[i++] = client.read();
}
ClientData[i] = '\0';
if (strcmp(ClientData, "L") == 0)
{
client.print(DistanceReading);
}
client.flush();
client.stop();
RequestFailureCounter = 0;
SS1.print("Request Received: ");
SS1.println(ClientData);
SS1.print("Reply sent: ");
SS1.println(DistanceReading);
}
else
{
SS1.println("No Request...");
}
}
Looking at the 1.02 ethernet examples; for the chat server they use "server.write(thisChar);" to return the response to the client whereas in the Webserver example they use "client.println();" to return the reponse. What are the pros and cons for each and which one should I use?
Also the webserver example includes the code:
while (client.connected()) {
if (client.available()) {
char c = client.read();
Inserting the "while (client.connected()) " in front of the "if (client.available())".
I am just running with the " if (client.available())" as in the chat server example.
Again, what are the pros and cons of of using the client.connected() or is using just the client.available() sufficient?