Created
November 26, 2013 04:54
-
-
Save limitusus/7653666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
int main(int argc, char** argv) { | |
int sock; | |
int ret; | |
struct sockaddr_un my_addr; | |
struct sockaddr_un peer_addr; | |
char* sock_path; | |
ssize_t recv_size; | |
char buf[4096]; | |
if (argc != 2) { | |
fprintf(stderr, "usage: %s SOCK_PATH\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
sock_path = argv[1]; | |
sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
if (sock < 0 ) { | |
perror("socket"); | |
exit(EXIT_FAILURE); | |
} | |
memset(&my_addr, 0, sizeof(my_addr)); | |
my_addr.sun_family = AF_UNIX; | |
strncpy(my_addr.sun_path, sock_path, sizeof(my_addr.sun_path) - 1); | |
ret = connect(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr_un)); | |
if (ret < 0) { | |
perror("connect"); | |
exit(EXIT_FAILURE); | |
} | |
while(1) { | |
recv_size = recv(sock, buf, 4096, MSG_PEEK); | |
if (recv_size == 0) { | |
printf("EOF\n"); | |
break; | |
} else if (recv_size < 0) { | |
perror("recv"); | |
exit(EXIT_FAILURE); | |
} else { | |
printf("peek: %s\n", buf); | |
} | |
} | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment