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

前端 未结 6 1431
灰色年华
灰色年华 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 18:14

    You could check if the socket is still connected by trying to write to the file descriptor for each socket. Then if the return value of the write is -1 or if errno = EPIPE, you know that socket has been closed.
    for example

    int isSockStillConnected(int *fileDescriptors, int numFDs){
         int i,n;
         for (i=0;i

提交回复
热议问题