client:
client1:
server:
-
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 ; }