让 dotnet core 2.0.0 Socket.Listen() 支持 UNIX 域套接字?
Having dotnet core 2.0.0 Socket.Listen() support UNIX Domain Socket?
根据 https://github.com/dotnet/corefx/issues/10981,我从 corefx/src/System.Net.Sockets/tests/FunctionalTests/UnixDomainSocketTest.cs 复制了实现,并像下面这样在本地使用它。 (我在 Ubuntu 上使用 dotnet core 2.0.0)
Socket s = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified);
Console.WriteLine(s.SendBufferSize);
Console.WriteLine(s.ReceiveBufferSize);
var unixSocket = "./my.sock.1";
var ep = new UnixDomainSocketEndPoint(unixSocket);
Console.WriteLine(ep.AddressFamily);
try
{
System.IO.File.Delete(unixSocket);
s.Bind(ep);
s.Listen(5); // **Operation not supported**
while(true)
{
var newS = s.Accept();
byte[] content = new byte[1000];
var result = s.Receive(content);
Console.WriteLine(result);
Console.WriteLine(Encoding.Default.GetString(content));
newS.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
s.Close();
}
例外如下:
Operation not supported
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.Listen(Int32 backlog)
at test01.Program.Main(String[] args) in /home/nonstatic/code/temp/test01/Program.cs:line 106
是否有任何解决方案或变通方法可以让 dotnet core 在 Linux 中构建域套接字服务器?谢谢!
@stephentoub 好像回复了你的 corresponding GitHub issue。
Did you try SocketType.Stream instead of SocketType.Dgram?
根据 https://github.com/dotnet/corefx/issues/10981,我从 corefx/src/System.Net.Sockets/tests/FunctionalTests/UnixDomainSocketTest.cs 复制了实现,并像下面这样在本地使用它。 (我在 Ubuntu 上使用 dotnet core 2.0.0)
Socket s = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified);
Console.WriteLine(s.SendBufferSize);
Console.WriteLine(s.ReceiveBufferSize);
var unixSocket = "./my.sock.1";
var ep = new UnixDomainSocketEndPoint(unixSocket);
Console.WriteLine(ep.AddressFamily);
try
{
System.IO.File.Delete(unixSocket);
s.Bind(ep);
s.Listen(5); // **Operation not supported**
while(true)
{
var newS = s.Accept();
byte[] content = new byte[1000];
var result = s.Receive(content);
Console.WriteLine(result);
Console.WriteLine(Encoding.Default.GetString(content));
newS.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
s.Close();
}
例外如下:
Operation not supported
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.Listen(Int32 backlog)
at test01.Program.Main(String[] args) in /home/nonstatic/code/temp/test01/Program.cs:line 106
是否有任何解决方案或变通方法可以让 dotnet core 在 Linux 中构建域套接字服务器?谢谢!
@stephentoub 好像回复了你的 corresponding GitHub issue。
Did you try SocketType.Stream instead of SocketType.Dgram?