Unix Domain : connect() : No such file or directory

前端 未结 2 1657
闹比i
闹比i 2021-01-12 07:05

as stated in the title, my connect() call to an unix domain type socket with an according address results in the error ENOENT: no such file or directory

相关标签:
2条回答
  • 2021-01-12 07:28

    After figuring out that I was handling the sockets properly, I altered my code for connect() a little and now it works. I just added this line after the declaration of my variable:

    memset(&address, 0, sizeof(struct sockaddr_un));
    

    Does anyone know why I need to set the whole variable to 0 to make it work? Should I ask this in a new topic or can I ask this here?

    0 讨论(0)
  • 2021-01-12 07:37

    Quoting from the glibc manual:

    You should compute the LENGTH parameter for a socket address in the local namespace as the sum of the size of the sun_family component and the string length (not the allocation size!) of the file name string. This can be done using the macro SUN_LEN:

    • Macro: int SUN_LEN (_struct sockaddr_un *_ PTR)
      The macro computes the length of socket address in the local namespace.

    The example which follows uses a computation for which you say it fails for you:

    size = (offsetof (struct sockaddr_un, sun_path)
           + strlen (name.sun_path) + 1);
    

    But you should try out that macro. If something changed, or the example is wrong, there is still a good chance that that macro works as intended. If it does, you can look at its innards. At a first glance, it seems to me that the macro lacks the + 1 part used in all the examples. Which matches the warning from the manual to use “not the allocation size!” As your post says this didn't work without + 1 either, chances are slim though.

    Out of curiosity, what's the length of the path? Have you checked whether the field provided in the structure is large enough to hold it? What is sizeof(address.sun_path) in your implementation? I wonder whether you might be copying to unreserved memory, and part of the path gets overwritten at the next function call.

    0 讨论(0)
提交回复
热议问题