title: Is there a way to detect that TCP socket has been closed by the remote peer, without reading from it?
body: <p>First, a little background to explain the motivation:  I'm working on a very simple select()-based TCP "mirror proxy", that allows two firewalled clients to talk to each other indirectly.  Both clients connect to this server, and as soon as both clients are connected, any TCP bytes sent to the server by client A is forwarded to client B, and vice-versa.</p>
  
  <p>This more or less works, with one slight gotcha:  if client A connects to the server and starts sending data before client B has connected, the server doesn't have anywhere to put the data.  I don't want to buffer it up in RAM, since that could end up using a lot of RAM; and I don't want to just drop the data either, as client B might need it.  So I go for the third option, which is to not select()-for-read-ready on client A's socket until client B has also connected.  That way client A just blocks until everything is ready to go.</p>
  
  <p>That more or less works too, but the side effect of not selecting-for-read-ready on client A's socket is that if client A decides to close his TCP connection to the server, the server doesn't get notified about that fact -- at least, not until client B comes along and the server finally selects-for-read-ready on client A's socket, reads any pending data, and then gets the socket-closed notification (i.e. recv() returning 0).</p>
  
  <p>I'd prefer it if the server had some way of knowing (in a timely manner) when client A closed his TCP connection.  Is there a way to know this?  Polling would be acceptable in this case (e.g. I could have select() wake up once a minute and call IsSocketStillConnected(sock) on all sockets, if such a function existed).</p>