Created
September 16, 2011 14:48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Hello World client | |
// Connects REQ socket to tcp://localhost:5555 | |
// Sends "Hello" to server, expects "World" back | |
// | |
#define ROLE "Client" | |
#include "common.h" | |
#define URL "tcp://localhost:5555" | |
int main (void) | |
{ | |
void *context = zmq_init (1); | |
printf ("Connecting to server...\n"); | |
void *requester = zmq_socket (context, ZMQ_REQ); | |
zmq_connect (requester, URL); | |
zmq_msg_t request, reply; | |
msgpack_sbuffer sbuf; | |
msgpack_packer_init (&mpkg, &sbuf, msgpack_sbuffer_write); | |
msgpack_zone mempool; | |
msgpack_zone_init(&mempool, 2048); | |
msgpack_packer mpkg; | |
msgpack_object deserialized; | |
int i; | |
for (i = 0; i != 10; i++) { | |
msgpack_sbuffer_init (&sbuf); | |
msgpack_pack_array (&mpkg, 2); | |
msgpack_pack_int (&mpkg, i); | |
msgpack_pack_str (&mpkg, "/i/am/a/clinet/"); | |
zmq_msg_init_size (&request, sbuf.size); | |
memcpy (zmq_msg_data (&request), sbuf.data, sbuf.size); | |
msgpack_unpack (sbuf.data, sbuf.size, | |
NULL, &mempool, &deserialized); | |
msg_info (deserialized, "Sending\t\t"); | |
zmq_send (requester, &request, 0); | |
zmq_msg_close (&request); | |
zmq_msg_init (&reply); | |
zmq_recv (requester, &reply, 0); | |
msgpack_unpack (zmq_msg_data(&reply), zmq_msg_size(&reply), | |
NULL, &mempool, &deserialized); | |
msg_info (deserialized, "Received Reply\t\t"); | |
zmq_msg_close (&reply); | |
} | |
msgpack_zone_destroy(&mempool); | |
msgpack_sbuffer_destroy(&sbuf); | |
zmq_close (requester); | |
zmq_term (context); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef COMMON_H | |
#define COMMON_H | |
#include <zmq.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <msgpack.h> | |
#define msg_info(d, t) do {\ | |
printf ("%s %s : ", ROLE, t);\ | |
msgpack_object_print (stdout, d);\ | |
puts ("");\ | |
} while (0) | |
#define msgpack_pack_str(p, s) do {\ | |
msgpack_pack_raw (p, sizeof(s));\ | |
msgpack_pack_raw_body (p, s, sizeof(s));\ | |
} while (0) | |
#endif /* COMMON_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS += -lzmq -lmsgpack -g | |
PROGS = server client | |
all: $(PROGS) | |
clean: | |
-rm -f $(PROGS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Hello World server | |
// Binds REP socket to tcp://*:5555 | |
// Expects "Hello" from client, replies with "World" | |
// | |
#define ROLE "Server" | |
#include "common.h" | |
int main (void) | |
{ | |
void *context = zmq_init (1); | |
void *responder = zmq_socket (context, ZMQ_REP); | |
zmq_bind (responder, "tcp://*:5555"); | |
zmq_msg_t request, reply; | |
msgpack_sbuffer sbuf; | |
msgpack_sbuffer_init (&sbuf); | |
msgpack_zone mempool; | |
msgpack_zone_init(&mempool, 2048); | |
msgpack_packer mpkg; | |
msgpack_object deserialized; | |
while (1) { | |
zmq_msg_init (&request); | |
zmq_recv (responder, &request, 0); | |
msgpack_unpack (zmq_msg_data(&request), zmq_msg_size(&request), | |
NULL, &mempool, &deserialized); | |
msg_info (deserialized, "Recieved\t\t"); | |
zmq_msg_close (&request); | |
sleep (1); | |
msgpack_packer_init (&mpkg, &sbuf, msgpack_sbuffer_write); | |
msgpack_pack_array (&mpkg, 2); | |
msgpack_pack_int (&mpkg, -1); | |
msgpack_pack_str (&mpkg, "/i/am/a/server/"); | |
zmq_msg_init_size (&reply, sbuf.size); | |
memcpy (zmq_msg_data (&reply), sbuf.data, sbuf.size); | |
msgpack_unpack (sbuf.data, sbuf.size, | |
NULL, &mempool, &deserialized); | |
msg_info (deserialized, "Sending Reply\t\t"); | |
zmq_send (responder, &reply, 0); | |
zmq_msg_close (&reply); | |
} | |
msgpack_zone_destroy(&mempool); | |
msgpack_sbuffer_destroy(&sbuf); | |
zmq_close (responder); | |
zmq_term (context); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment