title: C and Python - communicating with sockets
body: <p>I'm trying to use UNIX domain sockets to communicate between a C program, and a Python script. The Python script sends data via UNIX domain sockets to the C program.</p>
  
  <p>Here is the relevant code from my C program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/un.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;    
  #define UNIX_PATH_MAX 100
  
  int main(void)
  {
  struct sockaddr_un address;
  int socket_fd, connection_fd;
  socklen_t address_length;
  pid_t child;
  
  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (socket_fd &lt; 0){
      printf("socket() failed\n");
      return 1;
  } 
  
  unlink("/tmp/demo_socket");
  memset(&amp;address, 0, sizeof(struct sockaddr_un));
  
  address.sun_family = AF_UNIX;
  snprintf(address.sun_path, UNIX_PATH_MAX, "/tmp/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 LOOP-----------------------&gt;
  while((connection_fd = accept(socket_fd, 
                              (struct sockaddr *) &amp;address,
                              &amp;address_length)) &gt; -1)
  {
  .
  .
  .(doesn't get any further than this)
  </code></pre>
  
  <p>This is the python script I am using to send a message to the socket:</p>
  
  <pre><code>import socket
  
  s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  s.connect("/tmp/demo_socket")
  print "Sending..."
  s.send("Hello world")
  x = s.recv(1024)
  print x
  s.close()
  </code></pre>
  
  <p>The python script fails with the error "Broken Pipe". The C program never enters the while loop because the accept() function fails and returns '-1'. </p>
  
  <p>Why is accept() failing? And what can I do to make it succeed?</p>