Created
March 5, 2014 22:36
-
-
Save kotaroito/9378131 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stddef.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include "error.h" | |
#define SOCK_PATH "/tmp/hello.socket" | |
#define BUFFER_SIZE 1024 | |
int main(void) | |
{ | |
int fd, size; | |
struct sockaddr_un peer_addr; | |
socklen_t peer_addr_len; | |
char buf[BUFFER_SIZE]; | |
memset(&peer_addr, 0, sizeof(peer_addr)); | |
peer_addr.sun_family = AF_UNIX; | |
strcpy(peer_addr.sun_path, SOCK_PATH); | |
// create socket | |
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) | |
err_sys("socket"); | |
// connect socket | |
peer_addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(peer_addr.sun_path); | |
if(connect(fd, (struct sockaddr*)&peer_addr, peer_addr_len) < 0) | |
err_sys("connect"); | |
// write | |
strcpy(buf, "hello"); | |
write(fd, buf, strlen(buf)); | |
close(fd); | |
unlink(SOCK_PATH); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment