I have code:
#define SOCK_PATH "/Users/piotr/swigo.sock"
int ux_server_socket() {
const int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
printf("%s\n", strerror(errno));
return -1;
}
struct sockaddr_un addr;
bzero(&addr, sizeof(addr));
addr.sun_len = sizeof(addr);
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SOCK_PATH);
if (unlink(addr.sun_path) == -1) {
printf("unlink: %s\n", strerror(errno));
}
int retv = bind(fd, (struct sockaddr*)&addr, (socklen_t)SUN_LEN(&addr));
if (retv == -1) {
printf("bind: %s\n", strerror(errno));
return -1;
}
printf("valid socket: %d\n", fd);
return fd;
}
When I create a project in C language in Xcode, this code works without any problems (in the console appears: valid socket: 3). However, when I create a project in Swift (Cocoa App), I added a C file to the project with this function (and the appropriate file h and bridge header too) something does not work. I see messages in the console:
unlink: Operation not permitted
bind: Address already in use
Why doesn't this valid C code work in a Swift project — any ideas?