I'm writing a TCP server that can take 15 seconds or more to begin generating the body of a response to certain requests. Some clients like to close the connection at their end if the response takes more than a few seconds to complete. 
Since generating the response is very CPU-intensive, I'd prefer to halt the task the instant the client closes the connection. At present, I don't find this out until I send the first payload and receive various hang-up errors.
How can I detect that the peer has closed the connection without sending or receiving any data? That means for recv that all data remains in the kernel, or for send that no data is actually transmitted.
You can select with a timeout of zero, and read with the MSG_PEEK flag.
I think you really should explain what you precisely mean by "not reading", and why the other answer are not satisfying.
Detect socket hangup without sending or receiving?