zmq是基于tcp实现的吗_ZeroMQ实例-使用ZMQ(ZeroMQ)进行局域网内网络通信

本文介绍了如何使用ZeroMQ(ZMQ)进行基于TCP的网络通信,包括安装ZMQ、创建接收端和发送端的代码示例,展示了ZMQ在局域网内的消息传递应用。
摘要由CSDN通过智能技术生成

本文内容摘要:1)安装zeromq、2)实例说明使用zmq进行网络间的消息发送和接收

首先在机器中安装zmq库

步骤如下:

1)下载zeromq的源代码,ZeroMQ的官方网址:

注:在本文写作时,ZMQ版本已经升级到4.1.0,不过影响没多大

2)解压源文件

tar zxf zeromq-4.0.3.tar.gz

3)

3.1进入zmq目录并进行编译和安装

cd zeromq-4.0.3

3.2执行配置文件

./configure

3.3 进行编译

make

3.4 安装zmq

make install

4)现在开始使用zmq进行网络通信

4.1接收端代码

1 //包含zmq的头文件

2 #include

3 #include "stdio.h"

4

5 int main(int argc, char *argv[])6 {7 void * pCtx =NULL;8 void * pSock =NULL;9 const char * pAddr = "tcp://*:7766";10

11 //创建context,zmq的socket 需要在context上进行创建

12 if((pCtx = zmq_ctx_new()) ==NULL)13 {14 return 0;15 }16 //创建zmq socket ,socket目前有6中属性 ,这里使用dealer方式17 //具体使用方式请参考zmq官方文档(zmq手册)

18 if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) ==NULL)19 {20 zmq_ctx_destroy(pCtx);21 return 0;22 }23 int iRcvTimeout = 5000;//millsecond24 //设置zmq的接收超时时间为5秒

25 if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iRcvTimeout, sizeof(iRcvTimeout)) < 0)26 {27 zmq_close(pSock);28 zmq_ctx_destroy(pCtx);29 return 0;30 }31 //绑定地址 tcp://*:776632 //也就是使用tcp协议进行通信,使用网络端口 7766

33 if(zmq_bind(pSock, pAddr) < 0)34 {35 zmq_close(pSock);36 zmq_ctx_destroy(pCtx);37 return 0;38 }39 printf("bind at : %s\n", pAddr);40 while(1)41 {42 char szMsg[1024] = {0};43 printf("waitting...\n");44 errno = 0;45 //循环等待接收到来的消息,当超过5秒没有接到消息时,46 //zmq_recv函数返回错误信息 ,并使用zmq_strerror函数进行错误定位

47 if(zmq_recv(pSock, szMsg, sizeof(szMsg), 0) < 0)48 {49 printf("error = %s\n", zmq_strerror(errno));50 continue;51 }52 printf("received message : %s\n", szMsg);53 }54

55 return 0;56 }

4.2发送端代码

1 //包含zmq的头文件

2 #include

3 #include "stdio.h"

4

5 int main(int argc, char *argv[])6 {7 void * pCtx =NULL;8 void * pSock =NULL;9 //使用tcp协议进行通信,需要连接的目标机器IP地址为192.168.1.210 //通信使用的网络端口 为7766

11 const char * pAddr = "tcp://192.168.1.2:7766";12

13 //创建context

14 if((pCtx = zmq_ctx_new()) ==NULL)15 {16 return 0;17 }18 //创建socket

19 if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) ==NULL)20 {21 zmq_ctx_destroy(pCtx);22 return 0;23 }24 int iSndTimeout = 5000;//millsecond25 //设置接收超时

26 if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iSndTimeout, sizeof(iSndTimeout)) < 0)27 {28 zmq_close(pSock);29 zmq_ctx_destroy(pCtx);30 return 0;31 }32 //连接目标IP192.168.1.2,端口7766

33 if(zmq_connect(pSock, pAddr) < 0)34 {35 zmq_close(pSock);36 zmq_ctx_destroy(pCtx);37 return 0;38 }39 //循环发送消息

40 while(1)41 {42 static int i = 0;43 char szMsg[1024] = {0};44 snprintf(szMsg, sizeof(szMsg), "hello world : %3d", i++);45 printf("Enter to send...\n");46 if(zmq_send(pSock, szMsg, sizeof(szMsg), 0) < 0)47 {48 fprintf(stderr, "send message faild\n");49 continue;50 }51 printf("send message : [%s] succeed\n", szMsg);52 getchar();53 }54

55 return 0;56 }

5)在CentOS下编译通过,记得要加zmq的链接库 -lzmq

1 gcc -o recv recv.c -lzmq2 gcc -o send send.c -lzmq

6)在机器192.168.1.2上运行recv程序,在同一个局域网的另一台机器(同一台机器也可以)上运行send程序,结果如下

6.1接收端

1$ ./recv2 bind at : tcp://*:7766

3 waitting...4 received message : hello world : 0

5 waitting...6 received message : hello world : 1

7 waitting...8 received message : hello world : 2

9 waitting...10 received message : hello world : 3

11 waitting...12 received message : hello world : 4

13 waitting...14 received message : hello world : 5

15 waitting...

6.2 发送端

1 $ ./send2 Enter to send...3 send message : [hello world : 0] succeed4

5 Enter to send...6 send message : [hello world : 1] succeed7

8 Enter to send...9 send message : [hello world : 2] succeed10

11 Enter to send...12 send message : [hello world : 3] succeed13

14 Enter to send...15 send message : [hello world : 4] succeed16

17 Enter to send...18 send message : [hello world : 5] succeed

7)结束语

以上是zmq最基本的网络通讯实例,在此基础上可以进行更复杂的设计,写出一些网络聊天、文件传输等的网络软件。

作者:风波

mail : fengbohello@qq.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值