diff --git a/cpp/zmq/req-rep/Makefile b/cpp/zmq/req-rep/Makefile index b54c035..8b7fd46 100644 --- a/cpp/zmq/req-rep/Makefile +++ b/cpp/zmq/req-rep/Makefile @@ -1,11 +1,11 @@ server : server.cpp - g++ -o server server.cpp -lzmq + g++ -g -o server server.cpp -lzmq server_arg: server_arg_port.cpp - g++ -o server_arg server_arg_port.cpp -lzmq + g++ -g -o server_arg server_arg_port.cpp -lzmq client : client.cpp - g++ -o client client.cpp -lzmq + g++ -g -o client client.cpp -lzmq client_connect_to_many : client_connect_multiple_server.cpp - g++ -o client_connect_to_many client_connect_multiple_server.cpp -lzmq + g++ -g -o client_connect_to_many client_connect_multiple_server.cpp -lzmq all: server server_arg client client_connect_to_many client clean: diff --git a/cpp/zmq/req-rep/client_connect_multiple_server_timeout.cpp b/cpp/zmq/req-rep/client_connect_multiple_server_timeout.cpp new file mode 100644 index 0000000..9193453 --- /dev/null +++ b/cpp/zmq/req-rep/client_connect_multiple_server_timeout.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +using std::cerr; +using std::cout; +using std::endl; + +int main (int argc, char *argv[]) +{ + if (argc < 2){ + cerr << "Usage: " << argv[0] << " server1-port server2-port ... "; + exit(1); + } + + zmq::context_t context (1); + zmq::socket_t socket (context, ZMQ_REQ); + int timeout = 4000; + socket.setsockopt(ZMQ_SNDTIMEO, &timeout, sizeof(int)); + socket.setsockopt(ZMQ_RCVTIMEO, &timeout, sizeof(int)); + + int i = 1; + char transport[128]; + + while (i < argc) { + snprintf(transport, 128, "tcp://127.0.0.1:%s", argv[i++]); + socket.connect(transport); + } + + char request_data[1024]; + i = 0; + while (true){ + do { + zmq::message_t request (1024); + snprintf(request_data, 1024, "Hello %08d", i++); + memcpy ((void *) request.data (), request_data, strlen(request_data)); + if ( socket.send (request) == false) { + cout << "Some error in sending request " << endl; + continue; + } + break; + } while (true); + do { + zmq::message_t reply; + if (socket.recv (&reply) == 0) { + cout << "Some error in read " << endl; + continue; + } + cout << "Received " << (char *)reply.data() << endl; + break; + }while (true); + sleep(1); + } + socket.close(); + context.close(); + return 0; +}