Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.Listen() does not support UNIX Domain Socket #24065

Closed
NonStatic2014 opened this issue Nov 6, 2017 · 4 comments
Closed

Socket.Listen() does not support UNIX Domain Socket #24065

NonStatic2014 opened this issue Nov 6, 2017 · 4 comments
Labels
area-System.Net.Sockets os-linux Linux OS (any supported distro)
Milestone

Comments

@NonStatic2014
Copy link

According to https://github.com/dotnet/corefx/issues/10981, I copied the implementation from corefx/src/System.Net.Sockets/tests/FunctionalTests/UnixDomainSocketTest.cs and use it locally like below. (I'm using dotnet core 2.0.0 on Ubuntu)

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();
}

The exception is something like below:

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/klein/code/temp/test01/Program.cs:line 106

Is there any solution or workaround to let dotnet core build a domain socket server in Linux? Thanks!

@NonStatic2014 NonStatic2014 changed the title Socket.Listen() does not support UNIX d Socket.Listen() does not support UNIX Domain Socket Nov 6, 2017
@stephentoub
Copy link
Member

You specified SocketType.Dgram. The error you're getting isn't coming from .NET, it's coming from the OS, saying it doesn't support that configuration. Did you try SocketType.Stream instead of SocketType.Dgram?

@stephentoub
Copy link
Member

stephentoub commented Nov 7, 2017

e.g. this C program works on my Ubuntu 17.10 VM:

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>

int main(void)
{
    unlink("./demo_socket");

    int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if(socket_fd < 0) { perror("socket"); return 1; }

    struct sockaddr_un address;
    memset(&address, 0, sizeof(struct sockaddr_un));
    address.sun_family = AF_UNIX;
    snprintf(address.sun_path, sizeof(address.sun_path), "./demo_socket");

    if (bind(socket_fd, (struct sockaddr *)&address, sizeof(struct sockaddr_un)) != 0) { perror("bind"); return 1; }
    if (listen(socket_fd, 5) != 0) { perror("listen"); return 1; }

    printf("done\n");
    return 0;
}

and prints out "done", whereas changing that SOCK_STREAM to SOCK_DGRAM results in it printing out "listen: Operation not supported". listen returns EOPNOTSUPP when "The socket is not of a type that supports the listen() operation."

@NonStatic2014
Copy link
Author

Thank you Stephen! I tried with STREAM + IP protocol and it worked. For IPC, which protocol would be the best one if I'm using SOCK_STREAM and the data between processes will be up to 10KB?

@stephentoub
Copy link
Member

You'd need to read up on domain sockets. I'm not sure of what, if any, tradeoffs exist.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Sockets os-linux Linux OS (any supported distro)
Projects
None yet
Development

No branches or pull requests

3 participants