Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish a Win32 socket handle from other pipe handles?

Tags:

winapi

winsock

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.

like image 769
Per Mildner Avatar asked Oct 26 '22 06:10

Per Mildner


1 Answers

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.

like image 61
ralphtheninja Avatar answered Dec 16 '22 10:12

ralphtheninja