Af_Unix in Windows

AF_UNIX equivalent for Windows

I have found the answer.

The big difference is that the handle the waits a connection is the same that does communications to a client. I would have to create a new named pipe for the server to wait for the next client.

References:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365799%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365603%28v=vs.85%29.aspx

Is there an AF_UNIX support on windows for Windows/WSL Interop on .NET Core

You need the UnixDomainSocketEndPoint class.

Also note that this only works on WSL version 1 and not version 2.

Here's a quick working sample I just cobbled together. Note that there's no real error-handling or robust input validation.

You'll need to either supply a path at the command line, create a c:\tmp\wsl directory, or edit the paths in the code.

using System;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

namespace AFUnixTest
{
class Program
{
static UnixDomainSocketEndPoint _endpoint;
static Socket UnixSocket => new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
static bool IsLinux => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
static string _socketPath;
static byte[] _buffer = new byte[1024];

static void Main(string[] args)
{
_socketPath = IsLinux ? "/mnt/c/tmp/wsl/unix.sock" : "c:\\tmp\\wsl\\unix.sock";
if (args.Length == 0)
{
Usage();
return;
}
if (args.Length > 1) _socketPath = args[1];
_endpoint = new UnixDomainSocketEndPoint(_socketPath);
if (args[0].ToLower() == "-s")
{
Server();
}
else if (args[0].ToLower() == "-c")
{
Client();
}
else
{
Usage();
}
}

static void Usage()
{
Console.WriteLine("Usage:");
Console.WriteLine($" { AppDomain.CurrentDomain.FriendlyName } -s or -c [socket path]");
Console.WriteLine($" Socket path should be in { (IsLinux ? "Linux" : "Windows") } path format.");
}

static void Server()
{
if (File.Exists(_socketPath)) File.Delete(_socketPath);
Console.WriteLine("Waiting...");
using (Socket listener = UnixSocket)
{
listener.Bind(_endpoint);
listener.Listen(10);
using (Socket handler = listener.Accept())
{
while (true)
{
int bytesReceived = handler.Receive(_buffer);
string message = Encoding.ASCII.GetString(_buffer, 0, bytesReceived);
if (message.ToLower().StartsWith("exit")) break;
Console.WriteLine($"Received [{ message }].");
}
Console.WriteLine("Exit received. Exiting...");
handler.Shutdown(SocketShutdown.Both);
}
}
}

static void Client()
{
Console.WriteLine("Starting client. Enter text. \"Exit\" (without quotes) to quit.");
using (Socket sender = UnixSocket)
{
sender.Connect(_endpoint);
while (true)
{
string message = Console.ReadLine();
var output = Encoding.ASCII.GetBytes(message);
int bytesSent = sender.Send(output);
Console.WriteLine($"Sent [{ bytesSent }] bytes.");
if (message.ToLower().StartsWith("exit")) break;
}
sender.Shutdown(SocketShutdown.Both);
}
}
}
}

AF_UNIX socket in Linux above Windows WSL fails to bind to /mnt file: error 95, Operation not supported

In quite-the-coincidence, I was searching the WSL GitHub issues yesterday for information on the X11 socket, when I came across this issue (which is unrelated to my topic, but relevant to yours), regarding AF_UNIX support in WSL2, which caught my eye simply because it was closed recently as By Design.

In short, AF_UNIX isn't support under WSL2 at this time. If possible for your application, consider converting (or copying) the WSL distro to WSL1, where AF_UNIX is supported.

Alternatively, you could use a network connection between the Windows and Linux ends.

In which way can i make use of AF_UNIX defined in Winsock2 in Windows

The documentation on MSDN for the socket function says:

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.

So, unless you have installed a third-party service provider, you cannot use AF_UNIX.

net.Listen() on unix domain socket failed on windows server 2008

From AF_UNIX comes to Windows
:

Beginning in Insider Build 17063, you’ll be able to use the unix socket (AF_UNIX) address family on Windows to communicate between Win32 processes.

So, it looks like that there is no support for it in older versions of Windows like Windows 7 and Windows Server 2008 and even in older versions of Windows 10.

Unix domain socket bind failed in windows

The issue seems to be the SO_REUSEADDR socket option, which ASIO by default sets. Setting this option itself succeeds, but causes the subsequent bind to fail.

Construct the acceptor with reuse_addr = false, then the binding should succeed:

  local::stream_protocol::acceptor acceptor(my_io_context, server, false);

Which windows server version does support unix socket

Go by build numbers. The first version with build number over 17000 is Windows Server 2019. Windows Server 2016 is 14000.



Related Topics



Leave a reply



Submit