ZeroMq是一个开源的消息队列网络框架,支持进程内和进程间的通信。
源码地址:https://github.com/zeromq/libzmq
windows下的编译:
打开build/msvc,打开工程,编译报错,解决方案如下:
1、添加宏
ZMQ_IOTHREAD_POLLER_USE_SELECT
ZMQ_POLL_BASED_ON_SELECT
2、编译成功,链接出错
将src下的文件全部添加到工程src目录下
编译完成。
编程示例:
//1、publish、subscribe模式
void zmq_publish_server()
{
printf("server start\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* publisher = zmq_socket(context, ZMQ_PUB);
int rc = zmq_bind(publisher, "tcp://*:8888");
if (rc != 0)
{
printf("bind fail");
return;
}
while (true)
{
int zipcode, temperature, relhumidity;
zipcode = rand() % (100000);
temperature = rand() % (215) - 80;
relhumidity = rand() % (50) + 10;
// Send message to all subscribers
char update[65];
snprintf(update, 64, "%05d %d %d", zipcode, temperature, relhumidity);
memcpy(zmq_msg_data(&msg), update, strlen(update));
//int size = zmq_sendmsg(publisher, &msg, 0);
int size = zmq_send(publisher, update, sizeof(update),0);
Sleep(2000);
}
zmq_close(publisher);
zmq_ctx_destroy(context);
}
void zmq_subscribe_client()
{
printf("zmq_client start.\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_SUB);
int rc = zmq_connect(subscriber, "tcp://localhost:8888");
/*
ZMQ_SUBSCRIBE:创建消息过滤标志
ZMQ_SUBSCRIBE属性将会在ZMQ_SUB类型的socekt上创建一个新的消息过滤标志。
新建立的ZMQ_SUB类型socket会对进入socket的所有消息进行过滤,这样你就可以使用这个属性来建立最初的消息过滤项。
一个option_value的长度是0的过滤属性会订阅所有的广播消息。一个非空的option_value值会只订阅所有以option_value的值为前缀的消息。
一个ZMQ_SUB类型的socket可以附加多个过滤条件,只要一个消息符合过滤条件中的任何一个就会被接受。
*/
zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "666", 3);// 只接收消息开头为“666”的消息,必须添加该语句对消息滤波,否则接受不到消息
while (true)
{
int size = zmq_recvmsg(subscriber, &msg, 0);
if (size == -1)
continue;
printf("recv:%s\n", (char*)zmq_msg_data(&msg));
}
}
//2、push pull模式
void zmq_push_server()
{
printf("server start\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* publisher = zmq_socket(context, ZMQ_PUSH);
int rc = zmq_bind(publisher, "tcp://*:8888");
if (rc != 0)
{
printf("bind fail");
return;
}
while (true)
{
int zipcode, temperature, relhumidity;
zipcode = rand() % (100000);
temperature = rand() % (215) - 80;
relhumidity = rand() % (50) + 10;
// Send message to all subscribers
char update[65];
snprintf(update, 64, "%05d %d %d", zipcode, temperature, relhumidity);
memcpy(zmq_msg_data(&msg), update, strlen(update));
//int size = zmq_sendmsg(publisher, &msg, 0);
int size = zmq_send(publisher, update, sizeof(update), 0);
Sleep(2000);
}
zmq_close(publisher);
zmq_ctx_destroy(context);
}
void zmq_pull_client()
{
printf("zmq_client start.\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_PULL);
int rc = zmq_connect(subscriber, "tcp://localhost:8888");
while (true)
{
int size = zmq_recvmsg(subscriber, &msg, 0);
if (size == -1)
continue;
printf("recv:%s\n", (char*)zmq_msg_data(&msg));
}
}
//3、req reply模式
void zmq_reply_server()
{
printf("server start\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* publisher = zmq_socket(context, ZMQ_REP);
int rc = zmq_bind(publisher, "tcp://*:8888");
if (rc != 0)
{
printf("bind fail");
return;
}
while (true)
{
char buf[255];
zmq_recv(publisher, buf, 255, 0);
printf("server recv:%s\n", buf);
int zipcode, temperature, relhumidity;
zipcode = rand() % (100000);
temperature = rand() % (215) - 80;
relhumidity = rand() % (50) + 10;
// Send message to all subscribers
char update[65];
snprintf(update, 64, "%05d %d %d", zipcode, temperature, relhumidity);
memcpy(zmq_msg_data(&msg), update, strlen(update));
//int size = zmq_sendmsg(publisher, &msg, 0);
int size = zmq_send(publisher, update, sizeof(update), 0);
Sleep(2000);
}
zmq_close(publisher);
zmq_ctx_destroy(context);
}
void zmq_req_client()
{
printf("zmq_client start.\n");
zmq_msg_t msg;
zmq_msg_init(&msg);
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_REQ);
int rc = zmq_connect(subscriber, "tcp://localhost:8888");
while (true)
{
char buf[255] = "hello server";
zmq_send(subscriber, buf, strlen(buf) + 1, 0);
int size = zmq_recvmsg(subscriber, &msg, 0);
if (size == -1)
continue;
printf("client recv:%s\n", (char*)zmq_msg_data(&msg));
}
}