ESP32 WiFiClient Library – remotePort

Home / References / ESP32 Library / WiFiClientSecure

Description

The remotePort method in the ESP32 WiFiClient Library returns the port number of the remote server or peer to which the client is currently connected. This method is invaluable for confirming the destination port of a connection, aiding in debugging, or logging network interactions in your projects.


Syntax and Usage

The remotePort method is invoked on a WiFiClient object to retrieve the remote server’s port number. Below is the syntax and a code snippet demonstrating its usage:

uint16_t port = client.remotePort();

Here’s how this method can be used:

  • Without Arguments: Call the method on an active WiFiClient instance to obtain the remote port number as an integer.

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)

This method does not require any arguments. It relies entirely on the current connection state of the WiFiClient object.


Return Value

The remotePort method returns an unsigned 16-bit integer (uint16_t) representing the port number of the remote server. If the client is not connected, it typically returns 0.


Example Codes

Below is an example demonstrating how to use the remotePort method in a practical scenario. This corresponds to the usage outlined in Section 2.

Example: Retrieving and Displaying the Remote Port Number

This example connects an ESP32 to www.httpbin.org on port 80, retrieves the remote server’s port number using remotePort, and prints it to the Serial Monitor for confirmation.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * 
 * Description: This example connects an ESP32 to
 * www.httpbin.org on port 80, retrieves the remote
 * server’s port number using ESP32 WiFiClient Library's
 * remotePort, and prints it to the Serial Monitor for
 * confirmation.
 *
 * 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");

        // Get the remote port number
        uint16_t remotePortNum = client.remotePort();
        Serial.print("Remote Port Number: ");
        Serial.println(remotePortNum);

        // 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: The ESP32 connects to a WiFi network and establishes a TCP connection to www.httpbin.org on port 80. The remotePort method retrieves the server’s port number (expected to be 80), which is then printed to the Serial Monitor. This allows makers to verify the connection’s destination port before sending an HTTP request and processing the response.

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

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!

error: Content is protected !!