Speed up sending data via TCP WiFiClient

Hope you're having a good day,

I am trying to maximise TCP data transmission from a client to a server on PC.
The WiFiClient documentation does not seem to give examples how to write TCP data fast.

The example that uses:

if (!client.connect(hostIP, port)) and client.stop();

But I have found a faster way using:

  if (client.connected()){
        Serial.println("Connected connected");
    Serial.println("Not connected");
    client.connect(hostIP, port);

However, when I read the serial it says:

Why is the tcp not connected sometimes? And does this mean I loose the buff for that loop?

Thanks for your suggestions,

You should post your whole sketch as making comments on a snippet can be misleading .

However based on the small amount of code you have posted it seems obvious that you have the client.write in the wrong place and that it should be after the if block rather than within it. There should also be a check after the client.connect to make sure it has worked. The buff won't be lost at that point but it will not be sent according to the code snippet whether it is lost after we cannot tell.

Thanks for your reply and apologies for posting just the snippet.
Here is my full code:

#include <WiFi.h>

/* WiFi network name and password */
const char * ssid = "WiFi Guest_";
const char * pwd = "";

/* Ip address of server (PC) and udp port - same as python script */
const char * hostIP = "";
const int port = 1234;

// number of active channels and number of timesteps sent with one packet
#define numOfCh 8
#define numOfTs 64

int adcSample1 = 0b1111111111111110;
int adcSample2 = 0b1111111111111110;

WiFiClient client;

void sendTCP(long packet[numOfCh + 2][numOfTs / 2]);

void setup()
  Serial.print("Connecting to ");
  /* connect to your WiFi */
  WiFi.begin(ssid, pwd);
  /* wait until ESP32 connect to WiFi*/
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected with IP address: ");
void loop()
  long buff[numOfCh + 2][numOfTs / 2];
  for (int ts = 0; ts < numOfTs; ts++) {
    for (int ch = 0; ch < numOfCh; ch++) {
      if (ts % 2 == 0) {
        buff[ch][ts / 2] = adcSample1 << 16;
        if (ch == 0) {
          buff[8][ts / 2] = micros();
      else {
        buff[ch][ts / 2] += adcSample2;
        if (ch == 0) {
          buff[9][ts / 2] = micros();

  client.connect(hostIP, port);
  while (!client.connected()) {
    client.connect(hostIP, port);
    Serial.println("Not connected");
  client.write((uint8_t*)buff, sizeof(buff));

Is this what you mean? I am no longer receiving "Not connected"), however on the receiving end the packets are coming in at inconsistent speeds (even when I comment out serial.print())

(You can ignore the filling of the packet with a 2d array.)

You are connecting every time round the loop but never ending the connection, you need to bear in mind that there is a very limited number of TCP connections and once exceeded the client will need to make a decision on which one to replace with a new connection. It would be better if the first client.connect was not there - it should be connected in setup and only reconnect if it is not connected.

What other users of the WIFI network are there, are there any repeaters, if on 2.4GHz are there any cordless phones, microwaves or anything that could put noise on the frequency in use causing delays?

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