Unix sockets in c#

2019-07-22 04:39发布

问题:

I'm trying to employ Mono's UnixEndPoint, but fail before even getting to use it. I'm running Xamarind + .net 4.5 on a 64bit windows 7 system.

Here's a bit of code proving that not a single combination works:

foreach(SocketType st in Enum.GetValues(typeof(SocketType)))
    foreach(ProtocolType pt in Enum.GetValues(typeof (ProtocolType)))
    {
        try{
            socket = new Socket (AddressFamily.Unix, st, pt);
            Console.WriteLine("{0} {1}", st, pt);
        }
        catch{

        }
    }

Basically, all combinations produce a socket error of some sort.

Over here, someone used it apparently - mono, c#, sockets, performance - is this supposed to work on windows at all? I might just be very silly trying to use UNIX sockets on WINDOWS?

回答1:

I don't see any reason to expect this to work on Windows.

The AddressFamily value is passed directly by .NET's Socket class to the native WSASocket() function to create the unmanaged socket object. By default, only AddressFamily.InterNetwork and AddressFamily.InterNetworkV6 (corresponding to AF_INET and AF_INET6, respectively) are supported by Windows, unless you have other network providers installed (NetBIOS would be an obvious alternative that might be commonly found on a Windows installation).

From the documentation:

The values currently supported are AF_INET or AF_INET6, which are the Internet address family formats for IPv4 and IPv6. Other options for address family (AF_NETBIOS for use with NetBIOS, for example) are supported if a Windows Sockets service provider for the address family is installed.

It makes sense that AddressFamily.Unix would work on a Unix machine, but unless you have installed a provider on your Windows machine that provides specific support for that address family, it should in fact fail on that Windows machine.


Note: as of August 2018, Windows provides limited support for AF_UNIX sockets, to allow for specific Windows Subsystem for Linux scenarios. There are important restrictions, among those being: only stream-oriented sockets are supported; and ancillary data (used e.g. to pass file information or user credentials) is not supported. A given socket can be used to communicate within Windows, within a WSL instance, or between Windows and a WSL instance, but not for multiple of these scenarios.

For now, it appears that there continues to be no support in desktop .NET version, and only limited support in .NET Core. Once the UnixDomainSocketEndPoint support is finalized in .NET Core, I suppose it's possible that will work its way back to the desktop .NET version.

For more details, see: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ and https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/