C and Python - communicating with sockets C and Python - communicating with sockets unix unix

C and Python - communicating with sockets


Looking at an example I found, it seems that the length you pass bind should not count the terminating or padding nulls in the sockaddr_un.

Try:

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) != 0) {    printf("bind() failed\n");    return 1;}

P.S. Because of this, you don't need the memset call.


IIRC, address_length needs to be initialized to the size of sockaddr_un before the accept() call. During the call, it is loaded with the actual length of the peer address that is shoved into address.

Rgds,Martin