tags:

views:

326

answers:

2

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.

+1  A: 

Have you tried WSADuplicateSocket. Then just check WSAPROTOCOL_INFO to see if it is in fact a named pipe...

Matt Davison
No. I will try it, thanks.
+1  A: 

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.

Magnus Skog
Thanks. This might be safer than using the socket-specific functions (which have been known to hang for non-sockets).