Wifi server,client,tcp speed.

Hi

I have a problem with response time.

I send string to arduino but response time 5sec.

Conection speed response good response time 1sec maybe and send the connection welcome message.

What the wrong why sloow?

#include <ESP8266WiFi.h>
#include <WebSocketClient.h>
#include <WiFiClient.h>
 
/* Set these to your desired credentials. */
const char *AP_ssid = "Ecet";
const char *AP_passkey = "12345678";
 
int status = WL_IDLE_STATUS;
boolean alreadyConnected[2] ; // whether or not the client was connected previously
 
WiFiServer server(6000);
WiFiClient client ;

#define Config_Led D2
 
void setup()
{
    delay(1000);
    //Serial.begin(115200);
   // Serial.println();
   // Serial.println("\nConfiguring access point...");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(AP_ssid, AP_passkey);
    delay(10000);
    IPAddress myIP = WiFi.softAPIP();
   // Serial.print("AP IP address: ");
    //Serial.println(myIP);
 
   // Serial.println("\nStarting server...");
    // start the server:
    server.begin();
   // Serial.println("\nServer Started.");
}
 
void loop()
{
    String szoveg = "safe";
    unsigned int i;
  
    for (i= 0; i<2; i++)
    {
        // wait for a new client:
        if (!client)
        {
            client = server.available();
        }
        else
        {
            if (client.status() == CLOSED)
            {
                client.stop();
               // Serial.print("\nConnection closed on client: "  );
               // Serial.println(i);
               // Serial.println();
                alreadyConnected[i] = false;
            }
 
            else
            {
                if(!alreadyConnected[i]) // when the client sends the first byte, say hello:
                {
                    // clean out the input buffer:
                    client.flush();
                    //Serial.print("\nNew client: ");
                    client.println("Hello, client !");
                   // Serial.println(i);
                    alreadyConnected[i] = true;
                }
 
                if (client.available())
                {
                    // read the bytes incoming from the client:
                    String data =  client.readStringUntil('\n');
                    // echo the bytes back to the client:
                     
                    if(szoveg.compareTo(data)==0){
                         client.flush();
                         client.println("ok")+"\n";
                         
                      
                        //client[i].println("ok1");
                        //client[i].println("ok2");
                        //client[i].println("ok3");
                        //client[i].println("ok4");  
                        }
                        //Serial.println("A ket szoveg megegyezik");
                    else
                        //Serial.println("Nem egyformak");
 
                     
                     
                    // echo the bytes to the server as well:
                    client.flush();
                    client.println("nemok")+"\n";;
                    
                   
                   // Serial.println(data);
                   
                }  
               
            }
        }
    }
}

there is no point in stopping a closed client

readStringUntil will wait a second for the next byte. if n is not sent then you loose a second

flush() doesn’t clear the input buffer.
it immediately sends the data from output buffer.
https://www.arduino.cc/reference/en/language/functions/communication/stream/streamflush/

I don’t understand your algorithm with the alreadyConnected array.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

WiFiServer server(6000);
WiFiClient clients[2];

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

  Serial.println("\nConfiguring access point...");
  WiFi.mode(WIFI_AP);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.begin();
  Serial.println("\nServer Started.");
}

void loop() {
  String szoveg = "safe";

  for (int i = 0; i < 2; i++) {
    if (!clients[i]) {
      clients[i] = server.available();
      if (clients[i]) {
        clients[i].println("Hello, client !");
      }
    }
  }
  for (int i = 0; i < 2; i++) {
    if (clients[i] && clients[i].available()) {
      // read the bytes incoming from the client:
      String data = clients[i].readStringUntil('\n');

      if (szoveg.compareTo(data) == 0) {
        clients[i].println("ok");
      } else {
        clients[i].println("nemok");
      }
      Serial.println(data);
    }
  }
}

Thanks your answer.

Here is the response fast but below unfortunately yet until all of them 5 secundum.

for (int i = 0; i < 2; i++) {
    if (!clients[i]) {
      clients[i] = server.available();
      if (clients[i]) {
        clients[i].println("Hello, client !");
      }

rogers1976:
Thanks your answer.

Here is the response fast but below unfortunately yet until all of them 5 secundum.

for (int i = 0; i < 2; i++) {

if (!clients[i]) {
      clients[i] = server.available();
      if (clients[i]) {
        clients[i].println(“Hello, client !”);
      }

how do you send the data? send \n at the end

Yes with Android studio.

Thread3(String message) {
            this.message = message;
        }

        @Override
        public void run() {
            output.write(message);
            output.flush();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMessages.append("client: " + message + "\n");
                    etMessage.setText("");
                }
            });

is output.write(message); the sending to server?
has the sent message '\n' at the end?

you can test the server with telnet client

wowww thanks alll :smiley: :smiley: :smiley: :smiley: :smiley:

not sendig the '\n'

class Thread3 implements Runnable {
        private String message;

        Thread3(String message) {
            this.message = message;
        }

        @Override
        public void run() {
            output.write(message+'\n'); here :)))
            output.flush();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMessages.append("client: " + message + "\n");
                    etMessage.setText("");
                }
            });

thanks alll :slight_smile:

rogers1976:
wowww thanks alll :smiley: :smiley: :smiley: :smiley: :smiley:

not sendig the '\n'

class Thread3 implements Runnable {

private String message;

Thread3(String message) {
            this.message = message;
        }

@Override
        public void run() {
            output.write(message+'\n'); here :)))
            output.flush();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMessages.append("client: " + message + "\n");
                    etMessage.setText("");
                }
            });




thanks alll :)

readString/until('\n')
if the \n is not there the timeout waits a second for it