I am attempting to use UNIX domain electrical sockets to speak from a C program, along with a Python script. The Python script transmits data via UNIX domain electrical sockets towards the C program.

This is actually the relevant code from my C program:

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <sys/not.h>

#include <sys/types.h>

#include <sys/socket.h>

#define UNIX_PATH_MAX 100

int primary(void)

not address

int socket_fd, connection_fd

socklen_t address_length

pid_t child

socket_fd = socket(AF_UNIX, SOCK_STREAM, )

if (socket_fd < )


memset(&address, , sizeof(struct sockaddr_not))

address.sun_family = AF_UNIX

snprintf(address.sun_path, UNIX_PATH_MAX, "/tmp/demo_socket")

if (bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_not)) != ) 

if(listen(socket_fd, 5) != ) 

//----------------WHILE LOOP----------------------->

while((connection_fd = accept(socket_fd,

                            (struct sockaddr *) &address,

                            &address_length)) > -1)

does not have any beyond this)

This is actually the python script I'm using to transmit a note towards the socket:

import socket

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)


print "Delivering..."

s.send("Hello world")

x = s.recv(1024)

print x


The python script fails using the error "Damaged Pipe". The C program never makes its way into the while loop since the accept() function fails and returns '-1'.

Exactly why is accept() failing? And so what can I actually do to really make it succeed?

Searching at a good example I discovered, it appears the length you pass bind shouldn't count the terminating or padding nulls within the sockaddr_not.


size_t addrlen

const char* sockname = "/tmp/demo_socket"

/* ... */

address.sun_family = AF_UNIX

snprintf(address.sun_path, UNIX_PATH_MAX, sockname)

addrlen = sizeof(address.sun_family) + strlen(sockname)

if (bind(socket_fd, (struct sockaddr *) &address, addrlen) != ) 

P.S. Due to this, you do not need the memset call.

IIRC, address_length must be initialized to how big sockaddr_not prior to the accept() call. Throughout the phone call, it's packed with the particular entire peer address that's shoved into address.

Rgds, Martin