Handling two ports in a TCP connection

I wrote a sketch to control a Roboclaw motor controller via TCP. For one motor this solution is working perfectly.
The hardware is able to control two motors. I would like to make it possible that two clients can connect and the motors are identified by the TCP-port-number.
For example port 5000 is used for motor1 and port 5002 for motor2.
Can this be realized ?

probably

which arduino do you use ?

For testing I use a ESP32-S3 WROOM. But later it should run on some STM32 board that I presently don't have.

try something like this (typed here, fully untested)

#include <WiFi.h>

constexpr char* ssid = "SSID";
constexpr char* password = "PASSWORD";

WiFiServer servers[] = {{5000}, {5002}};
constexpr size_t portCount = sizeof servers / sizeof *servers;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.write('.');
    delay(500);
  }
  Serial.println("\nConnected to WiFi");
  
  for (auto& server : servers) server.begin();
  Serial.println("Servers started");
}

void loop() {
  for (auto& server : servers) {
    WiFiClient client = server.available();
    if (client) {
      Serial.println("Client connected");
      while (client.connected()) {
        if (client.available()) Serial.write(client.read());
      }
      delay(1);
      client.stop();
      Serial.println("Client disconnected");
    }
  }
}

Cool, thanks a lot ! I will test it.

A TCP connection has 2 IP addresses (client, server) and two port numbers (client, server). The server has a fixed port number so that clients know what to connect to. Usually clients use an ephemeral port number, but (with the right API) it's also possible to specify a fixed client port number.
Are the port numbers you mention (5000 and 5002) server port numbers, or client port numbers?
Is there a reason why a connecting client can't send, as it's first message after the TCP connection has been established, a message like "motor 1" or "motor 2"? Relying on port numbers seems a bit of an ususual method.

1 Like

more than one client can connect to a server on the same port

I think they are server port numbers. If a client wants to control motor1 it must connect to port 5000, for motor2 it's 5002 (the numbers are only examples).
I have this already running on a Raspberry Pi. It was easy, because there I had multitasking.

But how can I detect which motor is ment ? It is not in the controlling protokoll.

Do you control what is in the message?

No, unfortunately not.
Your program works fine for one connection per time, but I need it for both connected simultaneously.
The port- number can be detected via 'client.localPort()' so I know the motor concerned.

So I think it's on principle not possible to have more than one TCP-connection opened at a time. I have to find another solution.
Thank you all for helping me.

you can have more connections opened. the sketch above discards the connection in every loop, becuase client is a local variable

The ESP32 can technically listen on two ports simultaneously using its two cores and FreeRTOS threads (tasks), but whether this works depends on the specific libraries being used.

Many libraries for networking on the ESP32, such as WiFi and AsyncTCP , are probably not inherently thread-safe.

So without additional precautions it's unclear if you won't fall into an UB trap

Would it work with two client variables ?

I just heard about something called LW-IP Stack. It is open source and the person who gave me the hint managed to get several ports running simultaneously on a STM32 with it.
Now I have to find out how to do it.

Exactly what do you mean by "simultaneously"?
If there is only one physical connection then nothing will be simultaneous no matter how many cores you have.

there is TCP/IP stack already in the esp32.
esp32 supports 8 TCP connections.

see the WiFiTelnetToSerial example on how to handle multiple connected clients

This has to be handled by the TCP/IP stack. It must buffer the received data from the sockets and pass it to the clients in an ordered way.

Obviously, so what is your point.