Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/wait.h>
const int k_BufferSize = 255;
int main()
{
int pair_soc[2];
char buf[k_BufferSize];
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair_soc) == -1) {
perror("socketpair");
return 1;
}
puts("Full-duplex pipe");
if (!fork()) {
while (1) {
read(pair_soc[1], &buf, k_BufferSize);
printf("[Process 2]: <<< %s", buf);
printf("[Process 2]: >>> ");
fgets(buf, k_BufferSize, stdin);
write(pair_soc[1], &buf, k_BufferSize);
puts("");
}
} else {
while (1) {
printf("[Process 1]: >>> ");
fgets(buf, k_BufferSize, stdin);
write(pair_soc[0], buf, k_BufferSize);
puts("");
read(pair_soc[0], &buf, k_BufferSize);
printf("[Process 1]: <<< %s", buf);
}
wait(NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment