Funtion running more then once

Hello guys!

I’ve got a problem with my verify funtion that runs more then once and I can’t figure out the problem. The only thing I know is that it’s running correctly ( returning false or true once ) when the funtion is inside the setup() and not in the loop().

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x50, 0x35 };

IPAddress arduino(192,168,0,12);
IPAddress server(192,168,0,15);

EthernetClient client;

String username[] = {"Admin","Skut","Arbek","Kubda"};
String password[] = {"12344","stra","124ssgra","!#¤%"};

String readString;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac,arduino);
  Serial.println("Connecting...");
  if (client.connect(server, 8888)){
   Serial.println("Connected to Server");
  } else {
    Serial.println("Connecting failed");
  }
}

boolean verify(String firstText, String secondText){
    for(int i = 0; i <= 3 ; i++){
    if((username[i] == firstText ) && (password[i] == secondText)){ 
     return true;
    }
  }
  return false;
}

void clientRead() { 
  while (client.available()) {
    if (client.available() >0) {
      char c = client.read();
      readString += c;
    }
}
}

void loop(){
  if (client.available() > 0){
    readString = "";
    clientRead();
    Serial.println(readString);
    int n = readString.length();
    int commaIndex = readString.indexOf(';');
    int lastIndex = readString.lastIndexOf(n);
    String firstText = readString.substring(0, commaIndex);
    String secondText = readString.substring(commaIndex + 1 , lastIndex);
    Serial.println(firstText);
    Serial.println(secondText);
    Serial.println(verify(firstText, secondText));
  }

 if (!client.connected()){
   Serial.println("Server disconnected!");
   client.stop();
   client.flush();
   while(true);
 }
}

This is what I get when sending ( Admin;12344 ) from the server to the Arduino:

Connecting...
Connected to Server
Admin;12344
Admin
12344
1






0

So, on one pass through loop, you read the response from the server. It isn’t complete, but there is enough to determine that the data matches expected patterns.

On a subsequent pass, the rest of the data (possibly nothing more than the carriage return and line feed that indicates that the response is complete) arrives, so you verify that.

You really need an end-of-packet marker that indicates that the packet is complete, and you need to read and store data until that marker arrives, and THEN call verify.

Perhaps the server should respond with “Admin;12344!”, instead of “Admin;12344”.