There are a couple of ways of achieving that. You could for example, before you start sending the chunks, send a packet just containing the total length of the stream your going to be receiving. Then just keep waiting till that threshold has been reached. Alternatively if you have a fixed chunk size you can wait till you receive a chunk less than the expected size (you’ll then have to check whether at the end you need to send a zero length chunk for the case where the total stream size is an exact multiple of the chunk size). There’s no real advantage/disadvantage to either it just depends on what you’re most comfortable with.