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

xiaoxiao2021-02-28  43

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接收端代码

按 Ctrl+C 复制代码 //包含zmq的头文件  #include <zmq.h> #include "stdio.h" int main(int argc, char * argv[]) {     void * pCtx = NULL;     void * pSock = NULL;     const char * pAddr = "tcp://*:7766";     //创建context,zmq的socket 需要在context上进行创建      if((pCtx = zmq_ctx_new()) == NULL)     {         return 0;     }     //创建zmq socket ,socket目前有6中属性 ,这里使用dealer方式     //具体使用方式请参考zmq官方文档(zmq手册)      if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)     {         zmq_ctx_destroy(pCtx);         return 0;     }     int iRcvTimeout = 5000;// millsecond     //设置zmq的接收超时时间为5秒      if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iRcvTimeout, sizeof(iRcvTimeout)) < 0)     {         zmq_close(pSock);         zmq_ctx_destroy(pCtx);         return 0;     }     //绑定地址 tcp://*:7766      //也就是使用tcp协议进行通信,使用网络端口 7766     if(zmq_bind(pSock, pAddr) < 0)     {         zmq_close(pSock);         zmq_ctx_destroy(pCtx);         return 0;     }     printf("bind at : %s\n", pAddr);     while(1)     {         char szMsg[1024] = {0};         printf("waitting...\n");         errno = 0;         //循环等待接收到来的消息,当超过5秒没有接到消息时,         //zmq_recv函数返回错误信息 ,并使用zmq_strerror函数进行错误定位          if(zmq_recv(pSock, szMsg, sizeof(szMsg), 0) < 0)         {             printf("error = %s\n", zmq_strerror(errno));             continue;         }         printf("received message : %s\n", szMsg);     }     return 0;

}

 4.2发送端代码//包含zmq的头文件  #include <zmq.h> #include "stdio.h" int main(int argc, char * argv[]) {     void * pCtx = NULL;     void * pSock = NULL;     //使用tcp协议进行通信,需要连接的目标机器IP地址为192.168.1.2     //通信使用的网络端口 为7766      const char * pAddr = "tcp://192.168.1.2:7766";     //创建context      if((pCtx = zmq_ctx_new()) == NULL)     {         return 0;     }     //创建socket      if((pSock = zmq_socket(pCtx, ZMQ_DEALER)) == NULL)     {         zmq_ctx_destroy(pCtx);         return 0;     }     int iSndTimeout = 5000;// millsecond     //设置接收超时      if(zmq_setsockopt(pSock, ZMQ_RCVTIMEO, &iSndTimeout, sizeof(iSndTimeout)) < 0)     {         zmq_close(pSock);         zmq_ctx_destroy(pCtx);         return 0;     }     //连接目标IP192.168.1.2,端口7766      if(zmq_connect(pSock, pAddr) < 0)     {         zmq_close(pSock);         zmq_ctx_destroy(pCtx);         return 0;     }     //循环发送消息      while(1)     {         static int i = 0;         char szMsg[1024] = {0};         snprintf(szMsg, sizeof(szMsg), "hello world : =", i++);         printf("Enter to send...\n");         if(zmq_send(pSock, szMsg, sizeof(szMsg), 0) < 0)         {             fprintf(stderr, "send message faild\n");             continue;         }         printf("send message : [%s] succeed\n", szMsg);         getchar();     }     return 0; }

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最基本的网络通讯实例,在此基础上可以进行更复杂的设计,写出一些网络聊天、文件传输等的网络软件。

如何在Windows上使用ZeroMQ请看这里:http://www.cnblogs.com/fengbohello/p/4369082.html

 

更多 ZeroMQ API :http://www.cnblogs.com/fengbohello/p/4230135.html 

转载请注明原文地址: https://www.6miu.com/read-85679.html

最新回复(0)