Hi there,
I just started working with Arduino, and I had a question regarding the Ethernet library, servers and clients. I came across the web server example on the Arduino site, and had a question about the following code:
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("");
break;
}
}
I was confused about the client.print() statements. If the code has gotten to the end of the line and the line is blank, that means the server has finished reading the client's request. In the comments, it says that after this happens, the server sends a response back to the client. However, the function to do this in the code is client.print(), which from the Arduino site, told me is used to print data to the server from the client. I'm confused as to how client.print() is used to send something from the server to the client. Is there something I am missing/am I understanding the client-server relationship wrong? Am I right in assuming that the arduino that is hosting the webpage is the server, and the webpage is the client?
Thank you!