How to change size of readstring() in client.read() on Arduino Ethernet?

Hi there,
I have a problem with my code and sometimes stops running.Most of the times when I am sending large data strings. I think its because of the size of the client readstring().It just saves 50 charactes send from /GET.

I tried to find the readstring function to modify its size but it just says "size" in the Ethernetclient.cpp file. How can I set it? Where is it stored?

Does anybody know?

EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 150) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {
  ....................
...............
}
}
}

I use a character array of a fixed size to store the GET request from a client. Try this code.
http://playground.arduino.cc/Code/WebServerST
It is the variable tBuf[] that holds the request string.

I have a problem with my code and sometimes stops running.

And it will continue to have problems until you get rid of the String.

I tried to find the readstring function to modify its size

readString is an instance of the String class. It has methods, not functions. The readStrring instance takes care of its size by itself. You don't need to do anything to help it.

I managed to change all my scketch using arrays this time.
How can I search for a word in an array???
for example: if I have an array with size of 50, called char array[];
and I need to search for the word "key" how can I do it?
I am trying using if (strcmp,"key"==0) but its not the right one.

Can somebody hwlp me with the needed command?

Your "needed command" is in the web server code in the playground that I posted above. It searches for the r and s variables in the GET request.

How can I search for a word in an array???

There are so many assumptions in this question. How do you define a word?

You can use a number of string functions to search for a string in a string. None of them will know, or care, that the string being searched for meets your definition of a word.

if I have an array with size of 50, called char array[];
and I need to search for the word "key" how can I do it?

Is array properly NULL terminated? Have you looked at strstr()?

I am trying using if (strcmp,"key"==0) but its not the right one.

Not for finding a string in a string, unless the string being search is exactly the string be searched for, which seems unlikely. Or, it would be working.

I want to search for a string word in a char array!
the array is: //
and I need to search for some strings like: "username" & "test"

which is the right command?

i am trying with:

char tBuf[64];
char offarray[] ="off";
if(strstr(offarray,tBuf)!=0 )//checks for off

if(strstr(offarray,tBuf)==0 ) is always true

Did you bother looking at the documentation for strstr()? It does not return an int, like strcmp() does.

The strstr() function returns a pointer to the string found, if one was found.

Whether that pointer is, or is not NULL, is impossible to tell from your snippets or (lack of) serial output.

http://www.cplusplus.com/reference/cstring/strstr/
That's the link I found.It helped me with strstr but only when tBuf just contains "off" if it contains more info it can't find off.
Why is this happening?
I am not so familiar with string manipulation so I don't know much. I am trying to do that all day with no luck.

char offarray[] ="off";
char tBuf[64];

 if(strstr(tBuf,"off") != NULL)//checks for off

          {

            digitalWrite(9,HIGH);    // set pin 4 low
            Serial.println("Led On");
            Serial.println(tBuf);
            delay(500);

          }

What tBuf contains: "GET /?username=off&password=&submit=Log+In HTTP/1.1"

"if(strstr(tBuf,"off") != NULL)" never goes TRUE
but "if(strstr(tBuf,"GET") != NULL)" does because it starts with that...

I finally managed to do it!

Thanks PaulS once agan for your great help! XD

I finally managed to do it!

Great. It would be nice to mention how, in case anyone finds this thread later.

Thanks PaulS once agan for your great help!

All I did was kick your butt and make you think.

That's the right one!

char offarray[] ="off";
char tBuf[64];

 if(strstr(tBuf,offarray) != NULL)//checks for off

          {

            digitalWrite(9,HIGH);    // set pin 4 low
            Serial.println("Led On");
            Serial.println(tBuf);
            delay(500);

          }

Now I'm expieriencing a very strange problem.
The code from the previous link works like a charm but the code below doesn't...

char username[ ] = "admin"; // YOUR Username for Login
char password[ ] = "pass"; // YOUR Password for Login
int loggedin = 0;
char tBuf[64];


if((strstr(tBuf,"username") != NULL)  && (strstr(tBuf,"password") != NULL) )  {
  if((strstr(tBuf,username) == NULL)  || (strstr(tBuf,password) == NULL) ) {
    loggedin=0;
    Serial.println("Wrong");
  }
}

if((strstr(tBuf,"username") != NULL)  && (strstr(tBuf,"password") != NULL) ){ 
  if((strstr(tBuf,username) != NULL)  && (strstr(tBuf,password) != NULL ))//Successful login
  {
    loggedin=1;
    Serial.println("successful");   
  }    
}

Arduino hangs and I have to Reset it...

I'd use nested ifs, not compound ifs, so I could put Serial.print() statements in each block, to see which part is causing problems.

I'd share ALL the code, and the serial output.

Your mileage may vary.

This code works

void loop() {
Serial.println("Start...");   
.....code
.....code
 if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,username) >0  && strstr(tBuf,password) >0 )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }
}

This code doesn't work..... :frowning:
After it prints "St" and hangs.....i have to reset it. and goes the same.

void loop(){
Serial.println("Start...");   
.....code
.....code
if(strstr(tBuf,"off") !=NULL) {
 digitalWrite(9, LOW);
     Serial.println("Led Off");  
    }


    if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,username) >0  && strstr(tBuf,password) >0 )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }
}

The sketch runs smoothly if we have just 2 ifs. If I add the third one, no matter which is it, it needs reset. and it doesn;t print anything. Strange right?
I've serial prints in every if statement but it doesn;t print not even the first one.

    if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {
    if(strstr(tBuf,username) >0  && strstr(tBuf,password) >0 )//Successful login

Why are you back to expecting strstr() to return a numeric value?

i have set username as character

char username[]="admin";
char password[]="pass";

if(strstr(tBuf,username) != NULL  && strstr(tBuf,password) != NULL )//Successful login  DOENS'T WORK :(


if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login   WORKS

:slight_smile:

maybe I need to save it as another operator. not char

The strstr() function returns a pointer, not an integer. When you get over expecting strstr() to return an integer, you'll be happier.

I need characters from an array to be found & printed. not integers.

Thats the final conclusion...
too many strstr is getting you a headache. Is this something of the chips memory or something?

if(strstr(tBuf,"off") != NULL) {
       loggedin=0;
 digitalWrite(9, LOW);    // set pin 4 high
      Serial.println("Led Off!");

}

    if(strstr(tBuf,"admin") == NULL  || strstr(tBuf,"pass") == NULL ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

If I remove the code below everything works....

"if(strstr(tBuf,"off") != NULL) {
       loggedin=0;
 digitalWrite(9, LOW);    // set pin 4 high
      Serial.println("Led Off!");

}"

But when I add it to this one it needs to be reseted. it stops in the first loop.it doesn't do anything else.arduino freezes none of the serials print on monitor....

  if(strstr(tBuf,"admin") == NULL  || strstr(tBuf,"pass") == NULL ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

Why don't you just look for what must be there to authenticate. I would use logic like this:

char tBuf[64] = "GET /?user=admin&pwd=mypasswd&submit=Log+In HTTP/1.1";

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

  if(strstr(tBuf,"user=admin") != NULL && strstr(tBuf,"pwd=mypasswd") !=NULL) {
    Serial.println("Logged in");
    // do the rest of your stuff here
  }
  else Serial.println("Login failed");  
}

void loop() {
  
}