Initial setup of client/server messaging
This commit is contained in:
commit
c8f56b8b52
4 changed files with 96 additions and 0 deletions
9
client/Makefile
Normal file
9
client/Makefile
Normal file
|
@ -0,0 +1,9 @@
|
|||
#CXXFLAGS += -fpermissive
|
||||
LDFLAGS += -lzmq
|
||||
|
||||
all: main
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf main *~
|
||||
|
32
client/main.cpp
Normal file
32
client/main.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
*
|
||||
* http://api.zeromq.org/2-2:zmq
|
||||
*
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <zmq.hpp>
|
||||
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
int threads = 2;
|
||||
zmq::context_t ctx (threads);
|
||||
|
||||
if (!ctx) {
|
||||
std::cout << "zmq_init() failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
zmq::socket_t s (ctx, ZMQ_REQ);
|
||||
s.connect ("tcp://localhost:5555");
|
||||
zmq::message_t request (10);
|
||||
memset (request.data (), 0, request.size ());
|
||||
s.send (request);
|
||||
|
||||
// wait for a reply
|
||||
zmq::message_t reply;
|
||||
s.recv (&reply);
|
||||
|
||||
return 0;
|
||||
}
|
9
server/Makefile
Normal file
9
server/Makefile
Normal file
|
@ -0,0 +1,9 @@
|
|||
#CXXFLAGS += -fpermissive
|
||||
LDFLAGS += -lzmq -lpthread
|
||||
|
||||
all: main
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf main *~
|
||||
|
46
server/main.cpp
Normal file
46
server/main.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
|
||||
#include <zmq.hpp>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
void *worker_routine (void *arg)
|
||||
{
|
||||
zmq::context_t *ctx = (zmq::context_t*) arg;
|
||||
|
||||
zmq::socket_t s (*ctx, ZMQ_REP);
|
||||
s.connect ("inproc://workers");
|
||||
|
||||
while (true) {
|
||||
zmq::message_t request;
|
||||
s.recv (&request);
|
||||
|
||||
sleep (1);
|
||||
|
||||
zmq::message_t reply (10);
|
||||
memset (reply.data (), 0, reply.size ());
|
||||
s.send (reply);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char** av) {
|
||||
zmq::context_t ctx (1);
|
||||
|
||||
zmq::socket_t workers (ctx, ZMQ_XREQ);
|
||||
workers.bind ("inproc://workers");
|
||||
|
||||
zmq::socket_t clients (ctx, ZMQ_XREP);
|
||||
clients.bind ("tcp://lo:5555");
|
||||
|
||||
|
||||
for (int i = 0; i != 10; i++) {
|
||||
pthread_t worker;
|
||||
int rc = pthread_create (&worker, NULL, worker_routine, (void*) &ctx);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
||||
zmq::device (ZMQ_QUEUE, clients, workers);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue