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.
I'm thinking that perhaps you could attempt to call GetNamedPipeInfo() on your handle. If the call succeeds you know that the handle is a pipe handle, otherwise it must be a socket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With