Arduino Web Server- how client works?

Hello, ive got problem understanding how client works in web server on the Ethernet library arduino. Here is the sketch example:

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  Serial.print("server is at ");

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 =;
        // 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("<!DOCTYPE HTML>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(" is ");
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
    // give the web browser time to receive the data
    // close the connection:
    Serial.println("client disconnected");

My questions are:

  1. Where does the client request happens? Because we get respond from server with html code, so i guess we need to trigger it somewhere?
  2. What acts as client here exactly? I would appreciate detailed explaining.
  3. What does “if (client)” line mean?
  4. What happens here?:
EthernetClient client = server.available();

This line activates defines your server waiting

EthernetServer server(80);

you are then ready for incoming clients on port 80

But what acts as client in this case? And where is request from client to server?

A web browser connecting to your IP address on standard port 80 for example

This in the loop creates a client object and listens for an incoming request. If there is no request available client will be NULL

EthernetClient client = server.available();
  if (client) {

when you get a request client is non null - will be an instance of EthernetClient - and you enter the if

What about that?

EthernetClient client = server.available();

What is that line for, what happens here?

Just typed that above

Okay, i see. Does the client request goes automatically? Cuz we put "client.println" for the server response later, so where does the request for that response happens?

You probably need to read a bit about the http protocol

Your server gets a request that is embodied by the client connection, the client object first receives data from the browser - which you should do something with like analyze it to see what is being asked - and then you send an answer back to that request by using the client abstraction as a communication “tunnel” .

The communication back needs to respect the protocol expected by the client - here a web browser. So that’s what you “print” back (a HTML haeder, and empty line and your html text) then you end the connection as your answer to the request is complete

I understand it like this, when i enter the ip of the website (i enter it from browser) it makes "get" request right? So we answer to it by "printing" the answer?

Yes you get a GET request for the / url and you answer with a well formed html answered with the right header

Thats what i needed :slight_smile: Its not so obvious cuz theres no code about it. One more thing can you tell me what is currentLineIsBlank for in that program? And why do we read single character in that line?:

char c =;

A well formed http get request header will end with a blank line, so that’s what the code tracks
Characters arrive one by one, hence you read them as the come in

(See HTTP/1.1: Request)