int zocket_open(const char *pathname, int flags, mode_t mode) { char buf[1024]; char *port = "0", *ptr = buf; strncpy(buf, pathname, 1000); if (!memcmp(ptr , "socket://", 9)) { ptr = ptr+9; // skip uri prefix /* EXPERIMENTAL : UNIX SOCKET SUPPORT FOR LINUX */ #if __linux__ { struct stat st; /* TODO: add support for listen */ if (stat(ptr, &st) == 0) { if ((st.st_mode & S_IFMT) == S_IFSOCK) { /* is a socket file */ socket_fd = socket_unix_connect(ptr); } else socket_fd = -1; goto beach; } } #endif port = strchr(ptr, ':'); if (port == NULL) { printf("No port defined.\n"); return -1; } port[0] = '\0'; if (strlen(ptr)==0) { // LISTEN HERE return -1; } // connect socket_fd = socket_connect((char*)ptr, atoi(port+1)); beach: if (socket_fd>=0) { printf("Connected to: %s at port %d\n", ptr, atoi(port+1)); } else printf("Cannot connect to '%s' (%d)\n", ptr, atoi(port+1)); socket_buf = (unsigned char *)malloc(1); socket_bufsz = 0; config_set("file.write", "true"); buf[0] = '\0'; } return socket_fd; }
int main (int argc, char **argv) { char buffer[64]; int sock; ssize_t n; if ((sock = socket_unix_connect("test.sock")) < 0) return(1); n = send(sock, "Hello Server\n", 13, 0); printf("Send %d\n", n); memset(buffer, 0, 64); n = recv(sock, buffer, 64, 0); printf("RECV %d '%s'\n", n, buffer); close(sock); return(0); }