Socketpair()在C / Unix的 [英] Socketpair() in C/Unix

查看:165
本文介绍了Socketpair()在C / Unix的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要来回通信在同一系统上2应用程序。从我的研究,我相信这就是所谓的进程间通信和使用socketpair(的)是我的问题的最好方法。

我撕裂我的头发(直译)试图开始创建与C. socketpair()插座从我个人理解,插座是一个非常复杂的话题,我是一个新手C程序员肯定是没有帮助的情况

我GOOGLE了过去48小时,阅读教程等,但我仍然不能得到它。我理解这个概念,但code是太混乱。我读过这篇文章了几声:<一href=\"http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html\">http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html,但它不是很简单。

有人可以提供一些示例(如此简单第5年级的学生能理解),或指向我一个很好的教程?


解决方案

您可以使用 socketpair <​​/ code>仅在其中创建两个过程,就像这样:


  1. 通话 socketpair <​​/ code> - 现在你有两个插座文件描述符(一​​个单管的两端)

    • 提名一端是的的,一个是孩子的结束。这并不重要,只要做出选择,并坚持下去以后


  2. 通话 - 现在你有两个进程

    1. 如果回零,你是孩子。关闭的的文件描述符,保持的孩子的描述符,并把它作为管道的过程结束

    2. 如果返回非零,你是家长。关闭的孩子的文件描述符,保持的的之一,并使用它作为您的末端治理


  3. 您现在有两个过程,同样的管子分别有一个文件描述符重新presenting不同的目的。请注意,这两个进程都运行相同的程序,但是他们遵循不同的分支呼叫后。如果的的通话其插座,的孩子的将能够读取该数据的及其插座,反之亦然

下面是一个直接翻译成code:

 无效子(INT插座){
    为const char您好[] =你好家长,我的孩子;
    写(插座,你好,的sizeof(你好)); / * NB。这包括NUL * /
    / *出去,做幼稚的事情与管道的这一端* /
}无效的父(INT插座){
    / *做父母的事情。为此,喜欢读书的孩子的消息* /
    焦炭BUF [1024];
    INT N =读(插座,BUF,sizeof的(BUF));
    的printf(父收到%* s'的\\ N,N,BUF);
}无效socketfork(){
    INT FD [2];
    静态const int的parentsocket = 0;
    静态const int的childsocket = 1;
    将为pid_t PID;    / * 1.呼叫socketpair ... * /
    socketpair(PF_LOCAL,SOCK_STREAM,0,FD);    / * 2调用fork ... * /
    PID =叉();
    如果(PID == 0){/ * 2.1,如果叉回到零,你是孩子* /
        关闭(FD [parentsocket]); / *关闭父文件描述符* /
        子(FD [childsocket]);
    }其他{/ * 2.2 ......你是母公司* /
        关闭(FD [childsocket]); / *关闭子文件描述符* /
        父(FD [parentsocket]);
    }
    出口(0); / *在父母与子女的功能所做的一切* /
}

请注意,这只是样品code:我已经离开了所有的错误检查和明智的流协议


如果你希望两个的独立的程序进行通信(例如,你调用一个可执行的客户的和一个叫服务器的),你可以'T使用这种机制。相反,你可以:


  • 使用Unix套接字(其中一台主机上的IPC管由一个文件名标识 - 这仅适用于当的客户的和的服务器的在同一台机器上运行)

  • 或使用TCP / IP套接字(其中一个IP地址和端口识别管道和的客户的和的服务器的可以在不同的机器上)


如果你不这样做的专门的需要插座,你很高兴,要求的客户的和的服务器的在同一台机器上运行,你也可以使用共享内存或消息队列。

I have 2 applications on the same system that I need to communicate back and forth. From my research I believe this is called Inter Process Communication and the use of socketpair() is the best method for my problem.

I am tearing my hair out (literally) trying to get started with creating sockets with socketpair() in C. From what I understand, sockets are a very complex topic and me being a novice C programmer is surely not helping the situation.

I googled for the last 48 hours, read tutorials, etc, but I still can't get it. I understand the concept, but the code is just too confusing. I've read this article a few times: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html, but it's not simple enough.

Can someone provide some example (so simple a 5th grader could understand) or point me to a good tutorial?

解决方案

You can use socketpair only where you create both processes, like so:

  1. call socketpair - now you have two socket file descriptors (two ends of a single pipe)
    • nominate one end to be the parent and one to be the child end. It doesn't matter which, just make a choice and stick to it later
  2. call fork - now you have two processes

    1. if fork returned zero, you are the child. Close the parent file descriptor, keep the child descriptor, and use it as this process's end of the pipe
    2. if fork returned non-zero, you are the parent. Close the child file descriptor, keep the parent one and use it as your end of the pipe

  3. you now have two processes, each has one file descriptor representing different ends of the same pipe. Note that both processes are running the same program, but they followed a different branch after calling fork. If parent calls write on its socket, child will be able to read that data from its socket, and vice-versa

Here is a straight translation into code:

void child(int socket) {
    const char hello[] = "hello parent, I am child";
    write(socket, hello, sizeof(hello)); /* NB. this includes nul */
    /* go forth and do childish things with this end of the pipe */
}

void parent(int socket) {
    /* do parental things with this end, like reading the child's message */
    char buf[1024];
    int n = read(socket, buf, sizeof(buf));
    printf("parent received '%.*s'\n", n, buf);
}

void socketfork() {
    int fd[2];
    static const int parentsocket = 0;
    static const int childsocket = 1;
    pid_t pid;

    /* 1. call socketpair ... */
    socketpair(PF_LOCAL, SOCK_STREAM, 0, fd);

    /* 2. call fork ... */
    pid = fork();
    if (pid == 0) { /* 2.1 if fork returned zero, you are the child */
        close(fd[parentsocket]); /* Close the parent file descriptor */
        child(fd[childsocket]);
    } else { /* 2.2 ... you are the parent */
        close(fd[childsocket]); /* Close the child file descriptor */
        parent(fd[parentsocket]);
    }
    exit(0); /* do everything in the parent and child functions */
}

Please note that this is just sample code: I've left out all error-checking and a sensible stream protocol.


If you want two separate programs to communicate (eg. you have an executable called client, and one called server), you can't use this mechanism. Instead, you might:

  • use UNIX sockets (where an IPC pipe on one host is identified by a filename - this only works if client and server run on the same machine)
  • or use TCP/IP sockets (where an IP address and port identify the pipe, and the client and server can be on different machines)

If you don't specifically need sockets, and you're happy to require that client and server run on the same machine, you can also use shared memory, or message queues.

这篇关于Socketpair()在C / Unix的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆