I am trying to connect to unix domain socket created by another 开发者_如何学运维process. here is what i am doing. the file is exists on the file system when I running it.
socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (socketFd < 0) {
socketFd = -1;
return -1;
}
bzero(&address, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
size_t address_length = sizeof(address.sun_family) +
sprintf(address.sun_path, "/tmp/%s", COMMON_SOCKET);
connect(socketFd, (struct sockaddr *)&address, address_length);
i got error and errno is ENOENT. but doing ls on /tmp show me the file is there.
what do i do wrong ?
You're confused about address_length
. It should simply be:
size_t address_length = sizeof(address);
or really just:
connect(socketFd, (struct sockaddr *)&address, sizeof(address));
Since your on Mac OS X, you can try:
socketFD = socket(PF_LOCAL, SOCK_STREAM, 0);
PF_UNIX is deprecated on Snow Leopard. Not sure about AF_UNIX on Mac OS (at least the socket man page doesn't show it).
精彩评论