匿名本地网络实现进程间通信

#include "stdio.h"
#include "sys/socket.h"
#include "unistd.h"
 
int main()
{
    int s[2];
    socketpair(AF_LOCAL, SOCK_STREAM, 0, s);
    int n;
    char buf[100];
    if (fork()==0)
    {
        while ((n=read(s[0], buf, sizeof(buf)))>0)
        {
            int i;
            --n;
            for (i=0; i
            {
                char c = buf[i];
                buf[i] = buf[n-1-i];
                buf[n-1-i] = c;
            }
            write(s[0], buf, n+1);
        }
    }
    else
    {
        for(;;)
        {
            n = read(STDIN_FILENO, buf, sizeof(buf));
            if (buf[0] == 'q')
            {
                break;
            }
            write(s[1], buf, n);
            n=read(s[1], buf, sizeof(buf));
            write(STDOUT_FILENO, buf, n);
        }
        close(s[1]);
    }
    return 0;
}

你可能感兴趣的:(通信)