[SOLVED] Connection closed by foreign host in PUT request

I’m trying to send a PUT request with a JSON encoded string to a server using the Arduino Yun, but either the program hangs or returns a “Connection closed by foreign host” after getting the 200 OK response. My global variables take up 954 bytes (37%), so there should be room for my 250 char array. It seems like it’s possible to make the request as long as I do not generate the array and add the JSON data with the -d flag. I’m thinking it’s a memory problem, but I do not understand why it is able to send the request and getting the response before breaking down then. It never returns to the loop function.

// // This method posts the robots status to the server.
void putStatus(const char logString[]) {

  /* Create the JSON object to be sent to the server */

  char b[250] = "";
  char timestamp[20];
  strcat(b, "{\"timestamp\":");
  strcat(b, timestamp);
  strcat(b, ",\"log\":\"");
  strcat(b, logString);
  strcat(b, "\",");
  strcat(b, "\"position\":");
  char buf[4];
  sprintf(buf, "%d", beeCountDriver);
  strcat(b, buf);
  strcat(b, ",\"status\":");
  sprintf(buf, "%d", robotStatus);
  strcat(b, buf);
  strcat(b, ", \"completed\":\"");
  for (int i = 0; i < (nrOfBees - 1); i++) {
    sprintf(buf, "%d", nStatus[i]);
    strcat(b, buf);
  sprintf(buf, "%d\"", nStatus[nrOfBees-1]);
  strcat(b, buf);
  strcat(b, "}");

  /* Sending the command through the Linux interface */
  Process p;
  p.begin("curl"); // Initialize the curl linux command for sending HTTP requests
  p.addParameter(F("-X"));  p.addParameter(F("PUT")); // Change to a PUT request
  p.addParameter(feedbackEndpoint); // Add the URL parameter
  p.addParameter(F("-d")); p.addParameter(b); // Add robot data
  Console.println(F("Sending PUT request"));
  p.run();      // Run the process and wait for its termination

  while (p.available()>0) {
    const char c = p.read();
    sprintf(buf, "%d", nStatus[i]);
    strcat(b, buf);

This will generate a bunch of numbers all strung together without a separator between each one. Should there be some form of punctuation between each one? It would be a good idea to print out the final string in 'b' to see what it actually looks like.


The lack of separator is intended to cut down on space usage since the statuses simply are binary values.

I found my own problem to be in

char buf[4];
sprintf(buf, "%d\"", nStatus[nrOfBees-1]);

The escape character of course also counts as a character so here I had a buffer overflow when counting the terminating null character. Luckily I only spent 6 hours troubleshooting and redoing my code to fix this. Lesson learned: Always look for the simplest explanation for your problems first :D