client.print problem

Dear List,
I am trying to modify the ethernet chat server example to communicate with another program. It basically works but I have problems when I try to use the client.print command within part of the program. It causes the whole thing to crash.

I want to be able to receive data from the other program (this works fine), then separate it into two parts, ie a label and a data value and then send these two parts back to the server.

In the code below, it works until I try to uncomment the following lines:

// client.println("ControlStr is ");
// client.println(ControlStr);

and

// client.println("ValueStr is ");
// client.println(ValueStr);

Any attempt to use this command in this particular section of the code causes a problem.

Any help much appreciated

Bests,
Nick

/*
 

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


byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x89, 0x46 };
IPAddress ip(169,254,156,144);  // this determines the IP address of the arduino so for a direct connection it can be anything
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);

char inData[20]; // Allocate some space for the string

char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  // initialize the ethernet device

  Ethernet.begin(mac, ip);  // don't need gateway or subnet
  // start listening for clients
  server.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Chat server address: ");
  Serial.println(Ethernet.localIP());
}



int CommandCompare(char* MatchStr) {
  EthernetClient client = server.available();
     while (client.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
            inChar = client.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
        }
    }

    if (strcmp(inData,MatchStr)  == 0) {
       Serial.println("Match Found: ");
        for (int i=0;i<19;i++) {
        inData[i]=0;
    }
    index=0;
        return(0);
    }
    else {
      Serial.println("Match Not Found: ");  
       for (int i=0;i<19;i++) {
        inData[i]=0;
    }
    index=0;
      return(1);
    }
   
}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();
  int SpacePosn =0;
  int FoundSpace = 0;
  int EndPosn = 0;
  int i;
  
  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clear out the input buffer:
      client.flush();    
      Serial.println("We have a new client");
      client.println("Hello, client!");
      
      alreadyConnected = true;     
    }
      
    while (client.available() > 0) { // Don't read unless
                                   // there you know there is data
        if(index < 19) {// One less than the size of the array
        
          inChar = client.read(); // Read a character
          if (inChar == ' ') {  // if we find a space
            FoundSpace = 0;
            SpacePosn = index;
          } 
          if (inChar == ';') {  // if we find a semi-colon that says end of string
            EndPosn = index;
          }
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
       }
     }
     index=0;
    
    char ControlStr[SpacePosn];
    char ValueStr[EndPosn-(SpacePosn)];
    
    for (i=0;i<SpacePosn;i++) {
      ControlStr[i]=inData[i];
    }
      ControlStr[i+1]='\0';
      Serial.print("ControlStr is ");
      Serial.println(ControlStr);
 //   client.println("ControlStr is ");
 //   client.println(ControlStr);
     
    for (i=0;i<(EndPosn-(SpacePosn+1));i++) {
      ValueStr[i]=inData[i+(SpacePosn+1)];
    }
    ValueStr[i+1]='\0';  
    
    Serial.print("ValueStr is ");
    Serial.println(ValueStr);
//  client.println("ValueStr is ");
//  client.println(ValueStr);
    
    
    for (i=0;i<19;i++) {
      inData[i]=0;      
    }
    
 
    
  }   
  
}
    char ControlStr[SpacePosn];
    char ValueStr[EndPosn-(SpacePosn)];

The size of these arrays is determined at compile time, not at run time.

  int SpacePosn =0;
  int FoundSpace = 0;
  int EndPosn = 0;

As you can see, those arrays are not going to hold a lot of characters.

That is not the proper way to allocate those arrays. You need to determine the maximum size that they need to be, and statically size them, or use malloc() to dynamically allocate them. Don't forget, as you did here, that you need room for the terminating NULL.

Actually, Paul, you can do that. I only found that out recently (on this forum). Example:

volatile int foo = 0;
void setup ()
{
foo = 100;
char bar [foo];
Serial.begin (115200);
strcpy (bar, "Hi there");
Serial.println (bar);
}

void loop () {}

That compiles and runs OK.

The relevant code is:

char bar [foo];
  f2:	80 91 1a 01 	lds	r24, 0x011A  <--- get foo
  f6:	90 91 1b 01 	lds	r25, 0x011B
  fa:	2d b7       	in	r18, 0x3d	; 61    <--- get stack pointer
  fc:	3e b7       	in	r19, 0x3e	; 62
  fe:	28 1b       	sub	r18, r24    <--- adjust by contents of foo
 100:	39 0b       	sbc	r19, r25
 102:	0f b6       	in	r0, 0x3f	; 63   <--- save SREG
 104:	f8 94       	cli     <--- noInterrupts
 106:	3e bf       	out	0x3e, r19	; 62    <--- save adjusted stack (first byte)
 108:	0f be       	out	0x3f, r0	; 63   <--- restore SREG (next instruction will be executed before an interrupt)
 10a:	2d bf       	out	0x3d, r18	; 61   <--- keep saving stack (second byte)

As for the original problem, not sure what values are in the variables.