zeromq的多进程通讯的例子

client:
C/C++ code
   
   
#include < zmq.h > #include < string .h > #include < stdio.h > #include < unistd.h > 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 < zmq.h > #include < string .h > #include < stdio.h > #include < unistd.h > 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 < zmq.hpp > #include < zmq.h > #include < unistd.h > #include < stdio.h > #include < string .h > 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 ; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值