title: unix domain sockets on linux?
body: <p>The following unix domain socket client and server example from <a href="http://www.thomasstover.com/uds.html" rel="nofollow">http://www.thomasstover.com/uds.html</a> does not work as expected on my slackware linux box.  I get this output:</p>
  
  <pre><code>$ ./server1 
  $
  
  $ ./client1 
  MESSAGE FROM SERVER: hello from a client
  </code></pre>
  
  <p>I expected the server to print the message hello from client and the client to print hello from server.</p>
  
  <p>My OS and compiler are as these:</p>
  
  <pre><code>$ uname -a                                                                                                    
  Linux temeraire 2.6.37.6-smp #2 SMP Sat Apr 9 23:39:07 CDT 2011 i686 Intel(R) Xeon(R) CPU           E3110  @ 3.00GHz Ge
  nuineIntel GNU/Linux
  $ gcc -v
  Reading specs from /usr/lib/gcc/i486-slackware-linux/4.5.2/specs
  COLLECT_GCC=gcc
  COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i486-slackware-linux/4.5.2/lto-wrapper
  Target: i486-slackware-linux
  Configured with: ../gcc-4.5.2/configure --prefix=/usr --libdir=/usr/lib --mandir=/usr/man --infodir=/usr/info --enable-
  shared --enable-bootstrap --enable-languages=ada,c,c++,fortran,java,objc,lto --enable-threads=posix --enable-checking=r
  elease --with-system-zlib --with-python-dir=/lib/python2.6/site-packages --disable-libunwind-exceptions --enable-__cxa_
  atexit --enable-libssp --enable-lto --with-gnu-ld --verbose --with-arch=i486 --target=i486-slackware-linux --build=i486
  -slackware-linux --host=i486-slackware-linux
  Thread model: posix
  gcc version 4.5.2 (GCC) 
  </code></pre>
  
  <p>server.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int connection_handler(int connection_fd)
  {
   int nbytes;
   char buffer[256];
  
   nbytes = read(connection_fd, buffer, 256);
   buffer[nbytes] = 0;
  
   printf("MESSAGE FROM CLIENT: %s\n", buffer);
   nbytes = snprintf(buffer, 256, "hello from the server");
   write(connection_fd, buffer, nbytes);
  
   close(connection_fd);
   return 0;
  }
  
  int main(void)
  {
   struct sockaddr_un address;
   int socket_fd, connection_fd;
   socklen_t address_length;
   pid_t child;
  
   socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
   if(socket_fd &lt; 0)
   {
    printf("socket() failed\n");
    return 1;
   } 
  
   unlink("./demo_socket");
  
   /* start with a clean address structure */
   memset(&amp;address, 0, sizeof(struct sockaddr_un));
  
   address.sun_family = AF_UNIX;
   snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
  
   if(bind(socket_fd, 
           (struct sockaddr *) &amp;address, 
           sizeof(struct sockaddr_un)) != 0)
   {
    printf("bind() failed\n");
    return 1;
   }
  
   if(listen(socket_fd, 5) != 0)
   {
    printf("listen() failed\n");
    return 1;
   }
  
   while((connection_fd = accept(socket_fd, 
                                 (struct sockaddr *) &amp;address,
                                 &amp;address_length)) &gt; -1)
   {
    child = fork();
    if(child == 0)
    {
     /* now inside newly created connection handling process */
     return connection_handler(connection_fd);
    }
  
    /* still inside server process */
    close(connection_fd);
   }
  
   close(socket_fd);
   unlink("./demo_socket");
   return 0;
  }
  </code></pre>
  
  <p>client.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  int main(void)
  {
   struct sockaddr_un address;
   int  socket_fd, nbytes;
   char buffer[256];
  
   socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
   if(socket_fd &lt; 0)
   {
    printf("socket() failed\n");
    return 1;
   }
  
   /* start with a clean address structure */
   memset(&amp;address, 0, sizeof(struct sockaddr_un));
  
   address.sun_family = AF_UNIX;
   snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
  
   if(connect(socket_fd, 
              (struct sockaddr *) &amp;address, 
              sizeof(struct sockaddr_un)) != 0)
   {
    printf("connect() failed\n");
    return 1;
   }
  
   nbytes = snprintf(buffer, 256, "hello from a client");
   write(socket_fd, buffer, nbytes);
  
   nbytes = read(socket_fd, buffer, 256);
   buffer[nbytes] = 0;
  
   printf("MESSAGE FROM SERVER: %s\n", buffer);
  
   close(socket_fd);
  
   return 0;
  }
  </code></pre>