ZMQ(PUSH-PULL)
推拉模式,PUSH发送。PULL方接收。PUSH可以和多个PULL建立连接,PUSH发送的数据被顺序发送给PULL方。如果是多个PULL,假如第一条消息发送给PULL1,那么第二条消息就会发送给PULL2,第三条又会发给PULL1,一直循环。发送消息的时候也是按照这个顺序发送,保证数据能够准确到达目的地。
C++示例代码:
服务端(ventilator):
//server(ventilator)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<zmq.h>
#include<assert.h>
using namespace std;
/*----推拉模式-------
PUSH (ventilator)
|
------------
| | |
PULL PULL PULL (worker)
PUSH PUSH PUSH
------------
|
PULL (sink)
---------------------*/
int main()
{
void *context = zmq_ctx_new();
assert(context != NULL);
void *sender = zmq_socket(context,ZMQ_PUSH);
assert(sender != NULL);
int ret = zmq_bind(sender, "tcp://*:6666");
assert(ret == 0);
printf("Pleas put down Enter,when the works are ready:\n");
getchar();
int i = 0;
while (1)
{
Sleep(1000);
char sendBuf[64] = { 0 };
sprintf(sendBuf,"this if from ventilator's message-%d",i++);
int bytes = zmq_send(sender, sendBuf, sizeof(sendBuf), 0);
if (bytes <= 0)
{
return -1;
}
printf("[Server] Sended Reply Message content == %s\n", sendBuf);
}
return 0;
}
我自己测试了两个worker,测试的目的是看两个worker消息接收是不是满足消息是循环的。
worker1:
//worker1
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<zmq.h>
#include<assert.h>
using namespace std;
/*----推拉模式-------
PUSH (ventilator)
|
------------
| | |
PULL PULL PULL (worker)
PUSH PUSH PUSH
------------
|
PULL (sink)
---------------------*/
int main()
{
void *context = zmq_ctx_new();
assert(context != NULL);
void *sender = zmq_socket(context,ZMQ_PUSH);
assert(sender != NULL);
int ret = zmq_bind(sender, "tcp://*:6666");
assert(ret == 0);
printf("Pleas put down Enter,when the works are ready:\n");
getchar();
int i = 0;
while (1)
{
Sleep(1000);
char sendBuf[64] = { 0 };
sprintf(sendBuf,"this if from ventilator's message-%d",i++);
int bytes = zmq_send(sender, sendBuf, sizeof(sendBuf), 0);
if (bytes <= 0)
{
return -1;
}
printf("[Server] Sended Reply Message content == %s\n", sendBuf);
}
return 0;
}
worker2:
//worker2
#include<iostream>
#include<zmq.h>
#include<assert.h>
using namespace std;
/*----推拉模式-------
PUSH (ventilator)
|
------------
| | |
PULL PULL PULL (worker)
PUSH PUSH PUSH
------------
|
PULL (sink)
---------------------*/
int main()
{
void *context = zmq_ctx_new();
assert(context != NULL);
void *recver = zmq_socket(context, ZMQ_PULL);
assert(recver != NULL);
void *sender = zmq_socket(context, ZMQ_PUSH);
assert(sender != NULL);
int ret = zmq_connect(recver, "tcp://localhost:6666");
assert(ret == 0);
ret = zmq_connect(sender, "tcp://localhost:5555");
assert(ret == 0);
while (1)
{
char *recvBuf[64] = { 0 };
int bytes = zmq_recv(recver, recvBuf, sizeof(recvBuf), 0);
if (bytes <= 0)
{
return -1;
}
printf("[Recver] : %s\n", recvBuf);
bytes = zmq_send(sender, recvBuf, sizeof(recvBuf), 0);
if (bytes <= 0)
{
return -1;
}
printf("[Recver] : %s \n", recvBuf);
}
return 0;
}
客户端(sink):
//client(sink)
#include<iostream>
#include<zmq.h>
#include<assert.h>
using namespace std;
/*----推拉模式-------
PUSH (ventilator)
|
------------
| | |
PULL PULL PULL (worker)
PUSH PUSH PUSH
------------
|
PULL (sink)
---------------------*/
int main()
{
void *context = zmq_ctx_new();
assert(context != NULL);
void *socket = zmq_socket(context, ZMQ_PULL);
assert(socket != NULL);
int ret = zmq_bind(socket, "tcp://*:5555");
assert(ret == 0);
while (1)
{
char *resBuf[64] = { 0 };
int bytes = zmq_recv(socket, resBuf, sizeof(resBuf), 0);
if (bytes <= 0)
{
return -1;
}
printf("[Client] from server content == %s\n", resBuf);
}
return 0;
}
测试结果: