I've updated the code so that when it is done reading, it will print both the bytes read and also the expected bytes read. I doubt that is all that is needed to handle the actual cases, but it would at least cover the simple case where the client closes the connection.
If you need it to be more robust, give it a whack and post what you have, and I'll help with any problems you have.