master slave system with arduino ethernet shield and ESP8266 wemos D1 mini

Please could someone suggest how best to use TCP for a master/slave system. i have a controller(master) with a Uno and ethernet shield which I would like to send messages to a number of Wemos D1 mini boards (slaves). This only requires messages of a few characters sent from the controller to the slaves. The slaves should send a response back to the master to acknowledge receipt.

I tried this with UDP to start with which worked but seemed unreliable - the messages from the slaves back to the controller seemed to get blocked after some time.

I'm wondering if TCP may be a useful way to go but I'm getting confused about which should be the server and the client. it seems that the master could be a client and the slaves could each be servers. Does this make sense?

EthernetClient or WiFiClient object wraps a TCP socket. A normal TCP socket is connected to IP address and port. EthernetServer or WiFiServer starts a listening socket on a port. If server on listening socket is contacted by a remote client socket, it creates a local socket connected with the remote client socket on a free port and returns a Client object wrapping the socket. Everything you write or print to a Client is send to that one remote socket.

If one of your client boards creates a Client and connects it to IP address and port of the Server on your 'server' board, then you get there a Client from server.available() and this two Client objects are connected. What you write/print on one side you read from the Client object on the other side.

client socket

   if (client.connect(serverIP, PORT)) {
     client.print("request\n");
     String response = client.readStringUntil('\n');
     Serial.println(response);
     client.stop();
   }

server side

   WiFiClient client = server.available();
   if (client && client.connected()) {
     String request = client.readStringUntil('\n');
     Serial.println(request);
     client.print("response\n");
     client.stop();
   }