程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> udp-zeromq進程間的通信怎麼使用?

udp-zeromq進程間的通信怎麼使用?

編輯:編程綜合問答
zeromq進程間的通信怎麼使用?

我今天看了下zeromq,也用zeromq寫了幾個簡單的例子,然後看了下它的介紹,發送支持好多通信協議,TCP、UDP、IPC
網上講的例子都是講TCP通信的,我想知道如果用來實現進程間的通信,這個要怎麼用呢!

最佳回答:


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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved