ESP8266 Wifi with more than 1 listening TCP Port

Please assist, I have multuple Ip cameras that will send an alarm using TCP, it works very well with this using one WiFiServer server(port), but I'm not sure how to add a second listening port (WiFiServer server2(port2)) ?

/*

  • Copyright (c) 2018, circuits4you.com
  • All rights reserved.
  • Create a TCP Server on ESP8266 NodeMCU.
  • TCP Socket Server Send Receive Demo
    */

#include <ESP8266WiFi.h>

#define SendKey 0 //Button to send data Flash BTN on NodeMCU

int port = 1235; //Port number
int port2 = 1248; //Port2 number
WiFiServer server(port);
WiFiServer server2(port2);

//Server connect to WiFi Network
const char *ssid = "ssid"; //Enter your wifi SSID
const char *password = "password"; //Enter your wifi Password

int count=0;
//=======================================================================
// Power on setup
//=======================================================================
void setup()
{
Serial.begin(115200);
pinMode(SendKey,INPUT_PULLUP); //Btn to send data
Serial.println();

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //Connect to wifi

// Wait for connection
Serial.println("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}

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

Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.print("Open Telnet and connect to IP:");
Serial.print(WiFi.localIP());
Serial.print(" on port ");
Serial.println(port);
}
//=======================================================================
// Loop
//=======================================================================

void loop()
{
WiFiClient client = server.available();
WiFiClient client = server2.available();

if (client)
{
if(client.connected())
{
Serial.println("Client Connected");
}

while(client.connected())
{      
  while(client.available()>0)
  {
    // read data from the connected client
    Serial.write(client.read()); 
  }
  //Send Data to connected client
  while(Serial.available()>0)
  {
    client.write(Serial.read());
  }
}
client.stop();
Serial.println("");
Serial.println("Client disconnected");    

}

if (client2) 

{
if(client2.connected())
{
Serial.println("Client2 Connected");
}

while(client2.connected())
{      
  while(client2.available()>0)
  {
    // read data from the connected client
    Serial.write(client2.read()); 
  }
  //Send Data to connected client
  while(Serial.available()>0)
  {
    client2.write(Serial.read());
  }
}
client2.stop();
Serial.println("");
Serial.println("Client2 disconnected");    

}
}
//=======================================================================

The amount of data sent is only about 100 text characters.

you don't need an additional port. all clients can send data to the same port.

Ok so even if they send at the same time it should not be a problem?

no problem

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.