ZeroMQ实例-使用ZMQ(ZeroMQ)进行局域网内网络通信

简介:

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

首先在机器中安装zmq库

步骤如下:

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

    百度网盘的下载地址 : http://pan.baidu.com/s/1mg61em0 

    ZMQ API 的 百度网盘 下载地址 : http://pan.baidu.com/s/1jGDqXfS

    :在本文写作时,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 <zmq.h>
 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;// millsecond
24     //设置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://*:7766 
32     //也就是使用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 <zmq.h>
 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.2
10     //通信使用的网络端口 为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;// millsecond
25     //设置接收超时 
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 -lzmq
2 gcc -o send send.c -lzmq

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

 6.1接收端

复制代码
 1 $ ./recv 
 2 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 $ ./send 
 2 Enter to send...
 3 send message : [hello world :   0] succeed
 4 
 5 Enter to send...
 6 send message : [hello world :   1] succeed
 7 
 8 Enter to send...
 9 send message : [hello world :   2] succeed
10 
11 Enter to send...
12 send message : [hello world :   3] succeed
13 
14 Enter to send...
15 send message : [hello world :   4] succeed
16 
17 Enter to send...
18 send message : [hello world :   5] succeed
复制代码

7)结束语

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


本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4046686.html,如需转载请自行联系原作者

相关文章
|
1月前
|
算法 网络虚拟化 数据安全/隐私保护
计算机网络-局域网
计算机网络-局域网
57 0
|
4月前
|
物联网 网络虚拟化 数据安全/隐私保护
【计算机网络】无线局域网详解
【计算机网络】无线局域网详解
54 0
|
4月前
|
缓存 数据安全/隐私保护 网络架构
【计算机网络】局域网体系结构、以太网Ethernet详解
【计算机网络】局域网体系结构、以太网Ethernet详解
104 0
|
6月前
|
存储 监控 网络协议
【计算机网络】第三章 数据链路层(虚拟机与局域网)
【计算机网络】第三章 数据链路层(虚拟机与局域网)
|
6月前
|
数据采集 JSON API
C#网络爬虫实例:使用RestSharp获取Reddit首页的JSON数据并解析
C#网络爬虫实例:使用RestSharp获取Reddit首页的JSON数据并解析
|
28天前
|
机器学习/深度学习 PyTorch 算法框架/工具
卷积神经元网络中常用卷积核理解及基于Pytorch的实例应用(附完整代码)
卷积神经元网络中常用卷积核理解及基于Pytorch的实例应用(附完整代码)
20 0
|
2月前
|
安全 数据库 网络虚拟化
计算机网络:思科实验【4-生成树协议STP及虚拟局域网VLAN】
计算机网络:思科实验【4-生成树协议STP及虚拟局域网VLAN】
|
2月前
|
弹性计算 大数据 测试技术
2024年阿里云服务器价格购买价格表(实例配置价格+磁盘价格+网络宽带价格)
2024年阿里云服务器价格购买价格表来了!2024年阿里云服务器租用费用,轻量应用服务器和云服务器ECS优惠价格表,云服务器ECS经济型e实例2核2G、3M固定带宽99元一年、ECS u1实例2核4G、5M固定带宽、80G ESSD Entry盘优惠价格199元一年,轻量应用服务器2核2G3M带宽轻量服务器一年61元、2核4G4M带宽轻量服务器一年165元12个月、2核4G服务器30元3个月,幻兽帕鲁4核16G和8核32G服务器配置,云服务器ECS可以选择经济型e实例、通用算力u1实例、ECS计算型c7、通用型g7、c8i、g8i等企业级实例规格。今天分享阿里云服务器租用费用最新报价:
64 2
|
7月前
|
弹性计算 固态存储 数据可视化
2023年最新阿里云服务器价格表(实例配置价格+磁盘价格+网络宽带价格)
2023年阿里云服务器租用费用,轻量应用服务器和云服务器ECS优惠价格表,阿里云轻量应用服务器2核2G3M带宽轻量服务器一年108元,2核4G4M带宽轻量服务器一年297.98元12个月,云服务器ECS包括通用算力型u1、ECS计算型c7、通用型g7和内存型r7均有活动
505 0
|
2月前
|
缓存 负载均衡 网络协议
《网络是怎么样连接的》读书笔记 - 服务器端的局域网中(四)
《网络是怎么样连接的》读书笔记 - 服务器端的局域网中(四)
42 0

热门文章

最新文章