title: Unix Domain : connect() : No such file or directory
body: <p>as stated in the title, my <em>connect()</em> call to an unix domain type socket with an according address results in the error <strong>ENOENT: no such file or directory</strong>.</p>
  
  <p>The two sockets are properly initialized and the socket files are created and bound accordingly. The server and client sockets run in a different process, though the client process is fork()-ed and execl()-ed. This is also how I parse the address for the client and server socket, which I use for setting up the client socket. The server process is using pthreads.</p>
  
  <p>Here is my <em>connect()</em> attempt:</p>
  
  <pre><code>struct sockaddr_un address;
  address.sun_family = AF_UNIX;
  memcpy(address.sun_path, filepath.c_str(), filepath.length());
  address.sun_path[filepath.length()] = '\0';
  
  if(-1 == connect(this-&gt;unix_domain_descriptor_.descriptor(),       \
                  (struct sockaddr*)&amp;address,                       \
                  size))
  {
      global::ExitDebug(-1, "connect() failed", __FILE__, __LINE__);
      return -1;
  }
  </code></pre>
  
  <p>I tried out different values for size, such as:</p>
  
  <pre><code>//  this is from unix(7) man page. It doesn't work neither with nor without "+1"
  socklen_t size =  offsetof(struct sockaddr_un, sun_path);
            size += strlen(address.sun_path) + 1;
  </code></pre>
  
  <hr>
  
  <pre><code>//  this is from one of my books about linux programming
  socklen_t size = sizeof(address);
  </code></pre>
  
  <hr>
  
  <pre><code>//  this is from a sample code which I found at the internet
  socklen_t size = sizeof(address.sun_family) + strlen(address.sun_path);
  </code></pre>
  
  <hr>
  
  <pre><code>//  Update 1: 
  socklen_t size = SUN_LEN(&amp;address);
  </code></pre>
  
  <hr>
  
  <pre><code>//  this is what I tried out after looking into the declaration
  //  of struct sockaddr_un
  socklen_t size = strlen(address.sun_path);
  </code></pre>
  
  <hr>
  
  <p>Surprisingly, all initializations except the last one result in an <strong>EINVAL: invalid argument</strong> error for <em>connect()</em> and I get the <strong>ENOENT: no such file or directory</strong> only with the last one. I even tried out entire examples from the internet, but without success. And obviously, swapping socklen_t with size_t or <em>int</em> doesn't change anything.</p>
  
  <p>I already checked for this:</p>
  
  <ul>
  <li>address.sun_path contains the correct socket file path starting from the root directory</li>
  <li>address.sun_path has the length of 61 characters</li>
  <li>address.sun_family is set to AF_UNIX/AF_LOCAL</li>
  <li>address.sun_family has the size of 2 bytes</li>
  <li>no errors at creating and binding both sockets</li>
  <li>server socket is in listening state</li>
  <li>sizeof(address) returns 110 as it is supposed to be</li>
  </ul>
  
  <p>Now I was wondering why the man page example was not working and whether there had been changes that were not updated at <a href="http://linux.die.net" rel="nofollow noreferrer" title="linux.die.net">linux.die.net</a> or <a href="http://www.kernel.org" rel="nofollow noreferrer" title="www.kernel.org">www.kernel.org</a>. My OS is Debian Squeeze if it's from relevant.</p>
  
  <p>Any ideas what I am doing wrong? And how to solve it? If you need more code or have questions, then don't hesitate to ask me (though I don't need to state this probably, but this is my first post here >.&lt;).</p>
  
  <p><em>btw, sorry for my bad english</em></p>
  
  <h2>Update 2</h2>
  
  <p>Solved. I will post it in an extra answer below out of clarity.</p>