TCP: When can I assume all the data is available?

I have a question concerning how you know when a TCP message has been fully received.

First, am I just over thinking this? When I detect that client.available() has data, can I assume that the entire message is there?

If not (that is, the data comes in as packets are received), is there a special character that HTTP 1.1 uses to let me know that the data is complete?

Thanks!

Afraid not. A TCP interaction is pretty similar to any other conversation you would have with someone. You say hello, discuss a few points, someone says goodbye and you know the conversation is over. HTTP requests are a bit ruder and they walk away without saying goodbye by which I mean they disconnect the connection. So your code should look like.

While (tcpClient.Connected()) {
  if (tcpClient.Available()) {
    //do something with the available data
  }
}

There’s currently an issue with tcpClient on the Photon where it stays connected although the server has disconnected so adding a tmeout is a good idea. Hope that helps.

It should be noted that this behavior while classical for HTTP is not too common on the modern Internet, instead by setting a keep-alive header HTTP can use persistent TCP connections for multiple requests, then either the Content-Length signfies when the request should be done or chunking is used. See for example https://en.wikipedia.org/wiki/Chunked_transfer_encoding

Hi @jed,

Something I’ve seen done is to lead your TCP messages with a 2-byte length header. This way the recipient reads the first two bytes, and knows how much more they need to read and cache before the message is fully sent, etc, etc.

Thanks!
David