Is there a way to detect that TCP socket has been closed by the remote peer, without reading from it?

前端 未结 6 1432
灰色年华
灰色年华 2020-12-09 17:27

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

6条回答
  •  既然无缘
    2020-12-09 17:58

    If your proxy must be a general purpose proxy for any protocol, then you should handle also those clients which sends data and immediately calls close after the send (one way data transfer only).

    So if client A sends a data and closes the connection before the connection is opened to B, don't worry, just forward the data to B normally (when connection to B is opened).

    There is no need to implement special handling for this scenario.

    Your proxy will detect the closed connection when:

    • read returns zero after connection to B is opened and all pending data from A is read. or
    • your programs try to send data (from B) to A.

提交回复
热议问题