I need to determine whether a handle that my code did not create, for which GetFileType()==FILE_TYPE_PIPE
, is a socket or not. There does not seem to be an API for this.
I have tried the following. The general idea is to use a socket-specific function and treat failure as meaning non-socket.
getsockopt()
-- This was my first attempt. Unfortunately it seems to hang when called by many threads on the same (non-socket) handle.WSAEnumNetworkEvents()
-- this is what Gnulib does but will have undesirable side effects if the handle is a socket.getpeername()
-- this is what cygwin does but this will fail for some sockets too. Guessing whether an error implies socket-ness does not seem reliable and future safe.
I do not mind if the solution only work on some versions of Windows, e.g. Vista, I can always fall back to some other method in the general case.