Network TCP total length

I am testing a simple TCP server/client app using the Network support on mac os x. It basically works well except when the server sends a lot of data (definition of "a lot" is that the amount of data exceeds the maximumLength value in the connection.receive call). In this case the client will receive multiple segments (each of which has less than or equal maximumLength value). For example if the maximumLength is set to 30000 and the server sends 100000 bytes, the client receive loop will get around 4 segments each of which has length <= 30000.

My question is: is there some information that the client can examine from the connection after a receive to determine that more data is available?

Replies

Yes, generally you should keep calling receive until both isFinal is true on the contentContext and isComplete is set to true. Here, "final" means that this is the last message that will appear on this connection, which is always true for a connection with just TCP, and "complete" means that this is the last chunk of data for this particular message.

So you're all done when you've received the last bit of content from the last (and in this case only) message.

In any other situation where error is nil, there's potentially more data to be read and you should keep calling receive.

  • thanks for the reply but not sure it helps. The connection in my sample app is a "long-running" connection (intended to handle multiple requests for any given client) so in general isComplete is never true (until I send an "end" request) and the isFinal is sure on every receive.

    If I run the netcat (nc) tool from a terminal window to my server, it seems to know when the data is complete for a given request; it echoes back all the data for that request and then accepts next input line

Add a Comment