zoukankan      html  css  js  c++  java
  • 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
    ", pAddr);
    40     while(1)
    41     {
    42         char szMsg[1024] = {0};
    43         printf("waitting...
    ");
    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
    ", zmq_strerror(errno));
    50             continue;
    51         }
    52         printf("received message : %s
    ", 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...
    ");
    46         if(zmq_send(pSock, szMsg, sizeof(szMsg), 0) < 0)
    47         {
    48             fprintf(stderr, "send message faild
    ");
    49             continue;
    50         }
    51         printf("send message : [%s] succeed
    ", 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最基本的网络通讯实例,在此基础上可以进行更复杂的设计,写出一些网络聊天、文件传输等的网络软件。

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

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

    作者:风波

    mail : fengbohello@qq.com

  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4046686.html
Copyright © 2011-2022 走看看