Concept question regarding client.print()

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!

Client.print and println work both in server and client sketches.

Thanks for replying SurferTim. If client.print works for both server and client sketches, how does it know which one it is working in when you call it? In other words, how does it know it is going from server to client and not client to server? Also, if client.print works for both, why is there a separate command server.write that is just from server to client?

Thanks!

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.

Yes. The code knows that the client request has ended because the client always sends two carriage return/line feeds at the end. Each pair is encountered, trigger the end of reading of a record. The empty record means that the end of the request has been found.

In the comments, it says that after this happens, the server sends a response back to the client.

And the code does just that.

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.
The function can also be used that way. If the Arduino is acting as a client, it uses client.print() to send data to the server. If it is acting as a server, it uses client.print() to send data to the client.

One could wish that they had used different names. GoesInto and GoesOuta might have made the direction of communication clearer. GoesOuta.print() clearly means that the Arduino is sending data out. Whether that data is going out to a client, in response to a request, or to a server, in the form of a request, doesn't really matter.

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.

Hosting is the wrong way to think of it. A server "hosts a web site" only in so far as that is where some code that sends data, often a html file, runs. In the case of this code, the Arduino IS acting as a server, and sending n html file.

and the webpage is the client?

No. A browser, presumably running on a PC somewhere, is.

Thank you so much PaulS! What is the purpose of server.write() if client.print can be used both ways?

The print() and write() methods do different things. The print() methods converts the values to strings, for ints, longs, floats, etc., and sends the strings. The write() method does no conversion. It sends binary data.

If you are trying to send a picture, sending binary data is necessary.