Checking if IP, gateway and mask is valid.

Hello, I’m currently development a web server in ESP32-EVB Olimex Board and I’m having some trouble in order to configure the server, since the client can change the IP, gateway and mask in the interface of the web page I can’t find a proper solution in order to test the incoming input in order to configure the server connection.

I tried to make a function that connects to the new server IP and even if I send a bad IP (I.E 10.10.10.10) It returns that he make the connection and obtained a response:

The problem is, if I send a wrong IP the testClient Function will return true, but if I try to access to the web server interface it doesn’t resolve the connection. What can I do?

bool testClient(const char* serverName)
{
  HTTPClient http;
  // Your IP address with path or Domain name with URL path 
  http.begin(serverName);
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    http.end();
    return true;
  }
  http.end();
  return false;
}

HTTP Response code: 200

This is the full code

#include <HTTPClient.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

// Replace with your network credentials
const char* ssid = "xxx";
const char* password = "xxx";

AsyncWebServer server(80);

bool testClient(const char* serverName)
{
  HTTPClient http;
  // Your IP address with path or Domain name with URL path 
  http.begin(serverName);
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    http.end();
    return true;
  }
  http.end();
  return false;
}

void InicioEstatico(IPAddress localIp,IPAddress gateway,IPAddress mask){
  int counter = 0;
  WiFi.config(localIp, gateway, mask);
  while (status != WL_CONNECTED && counter < 10) {
    Serial.println("Conectando al WiFi..");
    status = WiFi.begin(ssid, password);
    delay(1000);
    counter++;
  }
  iniciarServidor();
  String serverName = "http://"+localIp.toString()+"/verificar";
  if (!testClient(serverName.c_str())){
    Serial.println("The connection failed, returning to DHCP");
    server.end();
    WiFi.disconnect(true);
    WiFi.config(0U, 0U, 0U);
    WiFi.begin(ssid, password);
    delay(1000);
    iniciarServidor();
  }
}

void iniciarServidor(){
 // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "MAINPAGE");
  });

  server.on("/verificar", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", "OK");
  });
  // Start server
  server.begin();
}

void setup(){
Serial.begin(115200);
IPAddress localIp(10, 10, 10, 10);
IPAddress gateway(10, 10, 10, 10);
IPAddress subnet(255, 255, 255, 0);
InicioEstatico(localIp,gateway,mask);
}

void loop(){
}

it sends the request to itself? then any IP is good

Well, yeah, but I want to check if IP is valid in order to set the web server interface.

@kordhrufan

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.