chengbaifengyun
chengbaifengyun
2015-01-16 07:01
采纳率: 44.4%
浏览 4.9k
已采纳

zeromq进程间的通信怎么使用?

我今天看了下zeromq,也用zeromq写了几个简单的例子,然后看了下它的介绍,发送支持好多通信协议,TCP、UDP、IPC
网上讲的例子都是讲TCP通信的,我想知道如果用来实现进程间的通信,这个要怎么用呢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • net_building
    net_building 2015-01-18 00:20
    已采纳

    client:
    C/C++ code

    #include
    #include
    #include
    #include

    int main ()
    {

    void *context = zmq_init (1); // Socket to talk to server

    printf ("Connecting to hello world server...\n");

    void *requester = zmq_socket (context, ZMQ_DEALER);
    //void *requester = zmq_socket (context, ZMQ_DEALER);

    zmq_connect (requester, "ipc://pub");
    //zmq_connect (requester, "tcp://localhost:5555");

    int request_nbr;

    while(1)
    {

    zmq_msg_t request;

    zmq_msg_init_data (&request, "Hello", 6, NULL, NULL);

    printf ("Sending request %d...\n", request_nbr);

    zmq_send (requester, &request, 0);

    zmq_msg_close (&request);
    sleep(1);

    /*zmq_msg_t reply;

    zmq_msg_init (&reply);

    zmq_recv (requester, &reply, 0);

    printf ("Received reply %d: [%s]\n", request_nbr, (char *) zmq_msg_data (&reply));

    zmq_msg_close (&reply); */
    }

    zmq_close (requester);

    zmq_term (context);

    return 0;
    }

    client1:
    C/C++ code

    #include
    #include
    #include
    #include

    int main ()
    {

    void *context1 = zmq_init (1); // Socket to talk to server

    printf ("Connecting to hello world server...\n");

    //void *requester = zmq_socket (context, ZMQ_REQ);

    void *requester1 = zmq_socket (context1, ZMQ_DEALER);

    //zmq_connect (requester1, "tcp://localhost:5556");

    zmq_connect (requester1, "ipc://pub");

    int request_nbr;

    while(1)
    {

    /*zmq_msg_t request;

    zmq_msg_init_data (&request, "wjl", 8, NULL, NULL);

    printf ("Sending request %d...\n", request_nbr);

    zmq_send (requester1, &request, 0);

    zmq_msg_close (&request);

    sleep(1); */

    zmq_msg_t reply;

    zmq_msg_init (&reply);

    zmq_recv (requester1, &reply, 0);

    printf ("Received reply %d: [%s]\n", request_nbr, (char *)
    zmq_msg_data (&reply));

    zmq_msg_close (&reply);

    }

    zmq_close (requester1);

    zmq_term (context1);

    return 0;
    }

    server:
    C/C++ code

    #include
    #include
    #include
    #include
    #include

    int main ()
    {

    // Prepare our context and socket

    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_DEALER);
    //void *responder = zmq_socket (context, ZMQ_DEALER);
    // zmq_bind(responder, "ipc://pub");

    socket.bind ("ipc://pub");
    //socket.bind ("ipc://pub1");
    //socket.bind("tcp://*:5555");
    //socket.bind("tcp://*:5556");

    while (true)
    {

    zmq::message_t request; // Wait for next request from client

    socket.recv (&request);

    printf ("Received request: [%s]\n", (char *) request.data ()); // Do some 'work'

    //sleep (1); // Send reply back to client

    //zmq::message_t reply (8);

    // memcpy ((void *) reply.data (), (char *) request.data (), 8);

    socket.send (request);
    }
    /*while (1) {
    // Wait for next request from client
    zmq_msg_t request;
    zmq_msg_init (&request);
    zmq_recv (responder, &request, 0);
    printf ("Received Hello\n");
    zmq_msg_close (&request);

        //  Do some 'work'
        sleep (1);
    
        //  Send reply back to client
        zmq_msg_t reply;
        zmq_msg_init_size (&reply, 5);
        memcpy (zmq_msg_data (&reply), "World", 5);
        zmq_send (responder, &reply, 0);
        zmq_msg_close (&reply);
    }
    //  We never get here but if we did, this would be how we end
    zmq_close (responder);
    zmq_term (context);*/
    

    return 0;
    }

    点赞 评论
  • net_building
    net_building 2015-01-16 08:32

    实现一个进程的多个线程内的对象消息共享,可以通过PUBSUB方式处理。nanomsg不错,可以多看看。

    点赞 评论

相关推荐