ESP32 WebServer Library – client()

Home / References / ESP32 Library / WebServer Library

Description

The client() method is a member function of the ESP32 WebServer class. It provides access to the underlying WiFiClient object that represents the network connection of the client currently making a request to the web server. This allows for more direct interaction with the client connection, such as reading raw request data, checking connection status, or getting client IP information, beyond the standard handling provided by the WebServer library’s argument parsing and response methods.

This method is typically used within the handler functions that you register with the server using methods like server.on() or server.onNotFound().

Syntax and Usage

The client() method is called on an instance of the WebServer class. It does not take any arguments.

WiFiClient client = server.client();
  • Usage: Call this method inside a request handler function after a client has connected and made a request. It gives you the specific WiFiClient object for that interaction, allowing you to use any methods available for the WiFiClient class (like remoteIP()read()connected(), etc.) on that specific client connection.

Argument(s)

This method does not require any arguments.

Return Value

The client() method returns a WiFiClient object. This object represents the active TCP connection from the web client that initiated the current request being processed by the server handler. You can use this object to perform lower-level operations on the client connection.

Example Codes

This example demonstrates how to use the client() method within a request handler to retrieve and display the IP address of the connecting client on the Serial Monitor.

  1. Include the necessary libraries: WiFi.h for WiFi connectivity and WebServer.h for creating the web server.
  2. Replace the placeholder WiFi credentials (YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD) with your actual network’s SSID and password.
  3. Initialize the Serial Monitor for debugging output and connect the ESP32 to your WiFi network.
  4. Create an instance of the WebServer class, specifying port 80 (the standard HTTP port).
  5. Define a function (handleRoot) that will be executed when a client requests the root URL (“/”).
  6. Inside the handleRoot function, call server.client() to obtain the WiFiClient object corresponding to the current incoming request.
  7. Use the remoteIP() method, which is part of the WiFiClient class, on the retrieved client object to get the IP address of the client making the request.
  8. Print the client’s IP address to the Serial Monitor for observation.
  9. Send a simple HTML response back to the client’s browser using server.send().
  10. Register the handleRoot function to handle requests for the root path (“/”) using server.on().
  11. Start the web server using server.begin().
  12. In the main loop(), continuously call server.handleClient(). This function checks for and processes any incoming client connections and requests.
  13. To run this example: Upload the code to your ESP32, open the Arduino IDE’s Serial Monitor (set to 115200 baud), wait for it to connect to WiFi and display its IP address, then enter that IP address into your web browser. Each time you access or refresh the page in your browser, you should see the IP address of the device running the browser printed in the Serial Monitor.
/*
 * Author: Avant Maker
 * Date: April 30, 2025
 * Version: 1.0
 * License: MIT 
 * 
 * Description: 
 * This example demonstrates how to use ESP32 WebServer Library's 
 * client() within a request handler to retrieve and display the
 * IP address of the connecting client on the Serial Monitor.
 *
 * Code Source:
 * This example code is sourced from the Comprehensive Guide
 * to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
 * For additional code examples and in-depth documentation related to
 * the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/home/all-about-esp32-arduino-core-library/
 *
 * AvantMaker.com, your premier destination for all things
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
 * to empowering makers, learners, and enthusiasts with
 * the resources they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <WebServer.h>

// Replace with your network credentials
const char* ssid = "your_SSID";          // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD";  // Replace with your Wi-Fi password

// Create a WebServer object on port 80
WebServer server(80);

// Function to handle the root request
void handleRoot() {
  // Get the client object for the current request
  WiFiClient client = server.client();

  // Check if the client object is valid (though client() usually returns a valid object within a handler)
  if (client) {
    // Get the IP address of the connected client
    IPAddress clientIP = client.remoteIP();
    
    // Print the client's IP address to the Serial Monitor
    Serial.print("Client connected with IP address: ");
    Serial.println(clientIP);

    // You could potentially do other things with the client object here,
    // like reading specific headers or raw data if needed.
    // For example: while(client.available()){ char c = client.read(); Serial.write(c); }
  } else {
    Serial.println("Could not get client object.");
  }

  // Send a response back to the client
  server.send(200, "text/html", "<h1>Hello from AvantMaker ESP32 Example Code</h1><p>Check Serial Monitor for your IP address.</p>");
}

void setup() {
  Serial.begin(115200);
  delay(100); // Wait for serial monitor

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Register the handler function for the root URL
  server.on("/", handleRoot); 

  // Start the server
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  // Handle incoming client requests
  server.handleClient();
  delay(2); // Allow RTOS tasks to run
}
error: Content is protected !!