Determine between socket and fd

后端 未结 4 676
挽巷
挽巷 2021-02-13 06:04

On unix everything is a file approach of function read(), write(), close() is not supported on Win32.

I

4条回答
  •  再見小時候
    2021-02-13 06:37

    If the Windows 'C' library has dup() you could try to dup it, which should fail for a socket but succeed for a file fd. So:

    int is_net_socket(fd)
    {
      return close(dup(fd)) != 0;
    }
    

    Warning: untested theory with untested dependency ;-) Note that this would return misleading results if you run out of fd's. Another side effect is that if it is a file it will be flushed and its directory entry updated. All in all it probably sucks frankly. I might even downvote it myself.

提交回复
热议问题