Ethernet client: how to know when all the data has been read?

What's the (or a) correct test for knowing when all the data has been read from the server? Can I simply use client.connected()? Does the server always disconnect when it is done sending? I'm assuming that client.available() could return 0 in cases where the server is slow, but this doesn't necessarily imply that there won't be more data coming in the future, the same way that available works for the Serial class. Please let me know if that's incorrect.

Do you control the server? If so, add some kind of end-of-data marker that you recognize.

Otherwise, the client disconnects when all the data has been sent to the Arduino, so, if client.connected() is false, the client finished sending data.

Hi Paul,

No; I don't control the server. I'm retrieving a page (via GET) from twitter. If I'm understanding correctly, you are confirming that I can rely on client.connected() returning false as an indication that the conversation is over, and the disconnect happens without my having to call client.stop(). I'm a little confused because you say that if client.connected() is false, the client finished sending data and my Arduino client sends very little "data" (just the GET request), and if connected() is going to return false after that, it won't work for me.

Thanks for this, and any clarification.

When you make a GET request from an Arduino client, the connection between the client and the server (twitter, in your case) remains active until all data from the server has been fetched. Then, the connection is closed. It is the state of that connection that client.connected() returns.

client.available() returns the amount of data that has been sent back but not yet read.

When client.connected() is true and client.available() is 0, the server is processing the request, but has not yet returned all the data that was asked for, or what has been returned so far has already been read.

When client.connected() is false and client.available() is greater than 0, the server has responded with all the data that is going to, and the connection has been closed, but all the data has not yet been read.

When client.connected() is false and client.available() is 0, all the data returned by the server has been read.

So, between the two functions, you know whether to expect/wait for more data, or not.

Good; thanks, PaulS. The doc, if I am reading/understanding it correctly, disagrees on one point. The doc for the connected method says,

Whether or not the client is connected. Note that a client is considered connected if the connection has been closed but there is still unread data.

My reading of that suggests that I will never see this case:

When client.connected() is false and client.available() is greater than 0, the server has responded with all the data that is going to, and the connection has been closed, but all the data has not yet been read.

So I think I can just use connected() alone, but want to know if anyone knows otherwise.