Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active July 14, 2019 13:20
Show Gist options
  • Save surinoel/0ecb242fc4f7f4847a94732b11605355 to your computer and use it in GitHub Desktop.
Save surinoel/0ecb242fc4f7f4847a94732b11605355 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
int socket_fd[2];
int status;
pid_t pid;
char buf[] = "hello world";
char line[100];
memset(line, 0, sizeof(line));
int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fd);
/*
socketpair(AF_UNIX, SOCK_STREAM, AF_LOCAL, socket_fd);
int socketpair(int domain, int type, int protocol, int sv[2]);
*/
if(ret == -1){
perror("socketpair error\n");
return -1;
}
// 1, 2, 3번은 각각 stdin(0), stdout(1), stderr(2)의 다음 번호인 3, 4 할당
printf("socket1 fd: %d\n", socket_fd[0]);
printf("socket1 fd: %d\n", socket_fd[1]);
if((pid = fork()) < 0) {
perror("fork() error\n");
return -1;
} else if(pid == 0) {
write(socket_fd[0], buf, strlen(buf) + 1);
printf("data send : %s\n", buf);
close(socket_fd[0]);
} else {
wait(&status);
read(socket_fd[1], line, 100);
printf("data recv : %s\n", line);
close(socket_fd[1]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment