TCP client server communication. I need clarifications...

I am creating a Home Alarm based on multiple WemosD1 mini (one server AP - and TCP/WEB server) and multiple clients.
What I need to understand about TCP communications is:
the server use “WiFiServer TCPserver()” and listen for incoming TCP connections and once the client connects, receive a TCP message and (in my case) reply with an ACK.
the client code that sends the message is:

String TCP_SendReceive(String stringToSend){
    int cursorpos;
    char response[2048];

    // Connects to the server
    WiFiClient client;
    if (!client.connect(serverIP, serverPort)) {
      return("connection failed");
    }
    // sends the message
    if (client.connected()) {
      client.println(stringToSend);
    }

    // wait for a server response
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 10000) {
        client.stop();
        return("NORESPONSE");
      }
    }
    // Read the response
    cursorpos=0;
    while (client.available()) {
      response[cursorpos] = static_cast<char>(client.read());
      cursorpos++;
    }
    response[cursorpos] = '\0';

    //return the response from the server
    return(response);
    client.stop();
}

But there times that is the server that needs to send a message to the clients (one client at a time, of course) and get a reply.
To achieve this, do I need to start a WiFi server in the clients too, listening for incoming connection? or maybe I should move the “client” variable to global and not to stop the connection?

It’s my first project so, maybe I am just saying shit but… I wish to understand and… wish you understand me :slight_smile:

Either:

Your clients also need to have servers, or

Your clients need to poll the server, that is, they need to periodically ask the server if the server has anything to send to them. Presumably, the server would respond with either “No, I do not currently have anything to send to you” or “Yes, I have data to send to you and here it is…”. Of course, the asking and the response are not really so wordy.

vaj4088:
Of course, the asking and the response are not really so wordy.

LOL... of course! :slight_smile:

waiting a reply... I was still googling about esp8266 and tcp... and I've read that the max number of simultaneous connections is 4! well... I have 5 clients, so I suppose It is better to close and re-open the connection each poll. Am I wrong?

The answer is... it depends! You have a good solution, but sometimes it may be better for the fifth client to periodically retry until one of the other clients decides to close the connection. It depends upon the circumstances. You know better than me what you are trying to accomplish, what the data rates are, how often the polling is going to occur, how long until a client might decide to close a connection, etc. etc.

It may be easier to try a strategy and see if it works rather than attempting to plan everything in advance. I am a believer in advanced planning but sometimes trial and error can be a better teacher.

Good Luck!

vaj4088:
I am a believer in advanced planning but sometimes trial and error can be a better teacher.

never heard more truthful words!
let's get into patience mode and... try....try....and again try! :wink: