How to pass large string between a server sketch and a client sketch?

edited December 2015 in Library Questions

Hi guys,

I'm trying to send a large json from a client to a server. The JSON is a 64x64 matrix where each cell has a RGB color. So it is basically a string of 24000+ characters including the separators.

Everything works fine if I keep the string lenght under 8000 characters (8 bit color), but when trying to pass to large one I only get partial data.

Here's a code sample of my draw()

void draw () {

  client = server.available();

  if ( client != null) {

      data = client.readString();

      if (data != null) {

        println (data);
        //json = new JSONData(data);

        //if (json.command != null)
        //  commandMap.get(json.command).execute(); 

        data = null;
      }
    }
  }

  // Some code
}

Is there something wrong with it? I'm quite a newbie with the client-server concept.

Thanks

Answers

  • There's a fairly lengthy Network tutorial that might help a little here.

    I must admit my knowledge in this area is also a little flaky; but my understanding of the server/client relationship is that it lends itself more to streaming snippets of data back and forth on the fly. That's one area where JSON doesn't work at the moment: the whole file has to be delivered before it can be parsed; and I believe streaming is a process that may be interrupted. Unfortunately the docs don't make it clear if this is indeed the case with readString(); or if there is a limit to the size of the String that can be passed...

    So I'm wondering whether a possible approach here would be for the client to read the file directly from the server (e.g. using loadJSONObject). You could then limit reliance on the client/server connection to initiating this download when appropriate: i.e. send a message from the server notifying the client that the file has updated and should be re-loaded...

  • Answer ✓

    I have manage to find a work around. I simply use the readStringUntil() method in which I send the character "~" at the end of the json.

    This way, I always received all the data.

Sign In or Register to comment.