Networking Reference
In-Depth Information
Let's say we want to receive the results of Company1 or the company name that we
pass as an argument. In that case, we could change our client code to the following:
//
// Stock Market Client
// Connects SUB socket to tcp://localhost:4040
// Collects stock exchange values
//
#include <stdlib.h>
#include <string.h>
#include "zmq.h"
int main (int argc, char const *argv[]) {
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_SUB);
const char* filter;
if(argc > 1) {
filter = argv[1];
} else {
filter = "Company1";
}
printf("Collecting stock information from the server.\n");
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter,
strlen(filter));
int i = 0;
for(i = 0; i < 10; i++) {
zmq_msg_t reply;
zmq_msg_init(&reply);
zmq_msg_recv(&reply, subscriber, 0);
int length = zmq_msg_size(&reply);
char* value = malloc(length + 1);
memcpy(value, zmq_msg_data(&reply), length);
zmq_msg_close(&reply);
 
Search MirCeyron ::




Custom Search