LocalSocket communication with Unix Domain in Android NDK
LocalSocket uses the Linux abstract namespace instead of the filesystem. In C these addresses are specified by prepending '\0' to the path.
const char name[] = "\0your.local.socket.address";
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
// size-1 because abstract socket names are *not* null terminated
memcpy(addr.sun_path, name, sizeof(name) - 1);
Also note that you should not pass sizeof(sockaddr_un)
to bind
or sendto
because all bytes following the '\0' character are interpreted as the abstract socket name. Calculate and pass the real size instead:
int res = sendto(sock, &data, sizeof(data), 0,
(struct sockaddr const *) &addr,
sizeof(addr.sun_family) + sizeof(name) - 1);