Skip to content

Commit

Permalink
Added some more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
geet authored and geet committed Sep 11, 2014
1 parent 4538fb5 commit b13d59d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cpp/zmq/req-rep/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server : server.cpp
g++ -o server server.cpp -lzmq
server_arg: server_arg_port.cpp
g++ -o server_arg server_arg_port.cpp -lzmq
client : client.cpp
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

all: server server_arg client client_connect_to_many client
clean:
rm -f server server_arg client client_connect_to_many client
40 changes: 40 additions & 0 deletions cpp/zmq/req-rep/client_connect_multiple_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>

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 i = 1;
char transport[128];

while (i < argc) {
snprintf(transport, 128, "tcp://127.0.0.1:%s", argv[i++]);
socket.connect(transport);
}

while (true){
zmq::message_t request (1024);
memcpy ((void *) request.data (), "Hello", 6);
socket.send (request);
zmq::message_t reply;
socket.recv (&reply);
cout << "Received " << (char *)reply.data() << endl;
sleep(1);
}
socket.close();
context.close();
return 0;
}
1 change: 0 additions & 1 deletion cpp/zmq/req-rep/server.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main ()
{
zmq::context_t context (1);
Expand Down
39 changes: 39 additions & 0 deletions cpp/zmq/req-rep/server_arg_port.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <zmq.hpp>
#include <string>
#include <iostream>

using std::cerr;
using std::cout;
using std::endl;

int main (int argc, char *argv[])
{
if (argc != 2) {
cerr << argv[0] << " " << "bind-port" << endl;
exit(1);
}

char transport[255] = "";
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
snprintf(transport, 255, "tcp://*:%s", argv[1]);
socket.bind (transport);

char response[512];
memset(response, 0, 512);
snprintf(response, 512, "Response from server lisetning on %s", argv[1]);
int i = strlen(response);
int times = 0;

while (true){
zmq::message_t request;
socket.recv (&request);
zmq::message_t reply (512);
snprintf(response + i , 512, " #%d", times++);
memcpy ((void *) reply.data (), response, strlen(response));
socket.send (reply);
}
socket.close();
context.close();
return 0;
}

0 comments on commit b13d59d

Please sign in to comment.