ZMQ(三、推拉模式)

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;
}

测试结果:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值