Home / References / ESP32 Library / WiFiClientSecure
Description
The setNoDelay
method in the ESP32 WiFiClient Library enables or disables the Nagle’s algorithm for TCP connections. By default, Nagle’s algorithm is enabled, which buffers small outgoing data packets to optimize network efficiency. Setting this to true
disables the buffering, ensuring data is sent immediately, which is useful for real-time applications where low latency is critical.
Syntax and Usage
The setNoDelay
method can be invoked on a WiFiClient
object to control the TCP transmission behavior. Below is the syntax and a code snippet demonstrating its usage:
client.setNoDelay(nodelay);
Here are the ways to use this method:
- With Argument (Boolean): Pass a boolean value to enable or disable Nagle’s algorithm. Use
true
to disable buffering (immediate sending) orfalse
to enable it (buffered sending).
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
- nodelay (bool): A boolean value that determines whether to disable Nagle’s algorithm. Set to
true
to send data immediately without buffering, orfalse
to allow buffering for efficiency.
Return Value
This method does not return a value. It configures the underlying TCP socket option (TCP_NODELAY
) directly and takes effect immediately on the current WiFiClient
instance.
Example Codes
Below is an example demonstrating how to use the setNoDelay
method in a practical scenario. This corresponds to the usage outlined in Section 2.
Example: Using setNoDelay with a WiFiClient Connection
This example connects an ESP32 to a WiFi network and sends an HTTP GET request to www.httpbin.org
with setNoDelay(true)
to ensure immediate data transmission. This is useful for applications requiring real-time responses.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example connects an ESP32 to
* a WiFi network and sends an HTTP GET request to
* www.httpbin.org with setNoDelay(true) to ensure
* immediate data transmission. This is useful for
* applications requiring real-time responses.
* License: MIT
*
* 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/references/esp32-arduino-core-index/
*
* 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>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
const char* host = "www.httpbin.org";
const int port = 80;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Create a WiFiClient instance
WiFiClient client;
// Connect to the server
if (client.connect(host, port)) {
Serial.println("Connected to server");
// Disable Nagle's algorithm for immediate sending
client.setNoDelay(true);
// Send HTTP GET request
client.println("GET /get HTTP/1.1");
client.println("Host: www.httpbin.org");
client.println("Connection: close");
client.println();
// Wait for server response
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("Disconnected");
} else {
Serial.println("Connection failed");
}
}
void loop() {
// Nothing to do here
}
Explanation: In this code, the ESP32 connects to a WiFi network and establishes a TCP connection to www.httpbin.org
. The setNoDelay(true)
call ensures that the HTTP request is sent immediately without buffering, reducing latency. The response from the server is then printed to the Serial Monitor.
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFiClient Library
- Connection
- Send Data
- Receive Data
- Config
- Status
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!