serial output corruption with ethernet arduino

Here is what I have so far.

/*
  Web Server

A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield. 

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

*/

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x28, 0x0E };
IPAddress ip(192,168,1,200);
IPAddress remoteserver(69,198,171,165); // forum.reefangel.com
EthernetClient clientout;
unsigned long lastmillisout=millis();
unsigned long lastmillisin=millis();
boolean sending=false;
char strout[400];

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(2000);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
//    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while (client.available()) {
        char c = client.read();
        if (sending==false) Serial.write(c);
        lastmillisin=millis();        
      }
      while (Serial.available()){
        char c = Serial.read();
        client.print(c);
        if (millis()-lastmillisin>2000)
        {
          Serial.println("Timeout incoming");
          Serial.flush();
          lastmillisin=millis();
          client.stop();
        }
      }
      if (millis()-lastmillisin>2000)
      {
        Serial.println("Timeout incoming");
        Serial.flush();
        lastmillisin=millis();
        client.stop();
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    //    Serial.println("client disonnected");
  }
  else
  {
    if (Serial.available())
    {
      memset(strout,0,sizeof(strout));
      int b=Serial.readBytesUntil('\n',strout,sizeof(strout));
      Serial.flush();
//      Serial.println(b);
      Serial.println("Connecting");
      if (b>10)
      {
        sending=true;
        Serial.print(strout);
        Serial.println(" HTTP/1.0");
        Serial.println();
        if (clientout.connect(remoteserver, 2000)) 
        {
          Serial.println("Connected");
          //        while (Serial.available()){
          //          char c = Serial.read();
          //          clientout.print(c);
          //        }
          clientout.print(strout);
          clientout.println(" HTTP/1.0");
          clientout.println();
          lastmillisout=millis();
        }
      }
    }
    while (clientout.available()) {
      char c = clientout.read();
      Serial.print(c);
      if (millis()-lastmillisout>8000)
      {
        Serial.println("Timeout connected");
        lastmillisout=millis();
        clientout.stop();
        sending=false;
      }
    }
    if (!clientout.connected())
    {
      clientout.stop();
      sending=false;
    }
  }
  if (millis()-lastmillisout>8000)
  {
    Serial.println("Timeout global");
    lastmillisout=millis();
    clientout.stop();
    sending=false;
  }

}

//GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//Host: forum.reefangel.com
//GET /status/submitp.asp?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//Host: forum.reefangel.com
//
//GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//Host: forum.reefangel.com
//Connection: Keep-Alive

I get corruption on the last end of the string:
80469877386224802550000HERE84/PWM>76<PWMD</RA *** TO HERE***

Any help would be appreciated!!

Moderator edit: CODE TAGS

First, get rid of every single one of those calls to Serial.flush(), unless you have a damned good reason for them. If so, you'd be the first, and I'd like to know what that reason is.

Second, how much free memory do you have? That 400 element array may mean that you don't have any, which could explain the corruption.

What is sending that data to the serial port?

Binary sketch size: 15,952 bytes (of a 32,256 byte maximum) Free

I don't have a good reason for the flush().

The data is from a reef angel head unit...

By testing COM6 and the GET /r99 command the output is perfect coming from the main head unit...

so the corruption is happening in the ethernet module itself.

so the corruption is happening in the ethernet module itself.

No. It is still most likely an out-of-memory condition. The amount of program space used has nothing to do with the amount of SRAM (where the array and a lot of other stuff goes) used.

You need to search for FreeMemory() and add that code to your sketch. Add a call to that function around the call to readBytesUntil().

Agreed. It is an out-of-memory condition

What is the fix I can do to correct this?

Use a smaller array. Read less data before sending. Use a larger Mega. There are a number of solutions.

drewpalmer04: What is the fix I can do to correct this?

Reduce the 400 character array that is consuming almost half of your available 2k RAM.

Move all client and serial.prints to PROGMEM like this: Serial.print(F("Now in PROGMEM"));

I'm pretty noob at this thing...

so if i decrease the array by half (200) could this help?

OR would you be kind enough to help me with the corrections?

so if i decrease the array by half (200) could this help?

Yes. You really need to determine the appropriate size for the array. How much data is the sender sending? Is it constant?

It is constant...but not sure how to tell how much is being sent

[quote author=James C4S link=topic=140715.msg1056949#msg1056949 date=1357316419]

drewpalmer04: What is the fix I can do to correct this?

Reduce the 400 character array that is consuming almost half of your available 2k RAM.

Move all client and serial.prints to PROGMEM like this: Serial.print(F("Now in PROGMEM")); [/quote]

Tried this and my server fails to start...so I have to back to my original

I totally agree its memory related...but I'm not sure how to fix this with what I've got...any help please?

any help please?

We're asking questions. You're not answering them.

Is our homework due today?

Lol don't know how to tell output size for the serial. What else do you need to know? No homework due today

You said that you have a sketch that just reads the serial data. So, after reading up the \n, how many characters are in the array? The readBytesUntil() method NULL terminates the array, so strlen() will tell you the length.

Thank you for the info. I'll look ASAP

Just what are you trying to do? Are you sending a get request to a server and trying to capture data from what is sent from the server?

Sending the GET command to the head unit. Then relay data to remote server. That server remotely displays data on a portal. But this code just sends GET then converts to the string output to relay it to the remote server.

But this code just sends GET then converts to the string output to relay it to the remote server.

If you are trying to pass a lot of html churn, then you might consider saving all the html text to an SD card, then sending the captured material from the SD card to the remote server.

Well it's not my server. I'm just trying to send the string to the server to display my data but the string gets corrupted at the end as seen in my above example