后台开发学习笔记(二十二、libzmq安装和简单使用)

ZeroMQ轻量级消息传递内核是一个库,该库扩展了标准套接字接口,具有传统上由专用消息传递中间件产品提供的功能。ZeroMQ套接字提供了异步消息队列,多种消息传递模式,消息过滤(订阅),对多种传输协议的无缝访问等等的抽象。

22.1 源码安装

其实是很不喜欢用源码安装的,比较源码安装是最麻烦的,不过也要尝试一下。

22.1.1 下载源码

下载的源码路径,是在githubzeromq/libzmq点击下载就可以了,然后就是解压,这些不用写了吧、

在安装之前,我们在github上浏览一下我们需要看到的安装信息:
在这里插入图片描述
还想看一下具体的安装情况,结果发现在github上只显示了一行,看INSTALL文档,看就看了,去试试。

22.1.2 安装

这个INSTALL这个文档我第一次看也有点乱,可能是第一次看到,以后多接触开源项目,多源码安装,应该就会比较熟悉了。

经过我的查看,安装步骤是在这里的:

Basic Installation
==================

Briefly, the shell commands `./configure; make; make install' should
configure, build, and install this package.  The following
more-detailed instructions are generic; see the `README' file for
instructions specific to this package.

   The `configure' shell script attempts to guess correct values for
various system-dependent variables used during compilation.  It uses
those values to create a `Makefile' in each directory of the package.
It may also create one or more `.h' files containing system-dependent
definitions.  Finally, it creates a shell script `config.status' that
you can run in the future to recreate the current configuration, and a
file `config.log' containing compiler output (useful mainly for
debugging `configure').

   It can also use an optional file (typically called `config.cache'
and enabled with `--cache-file=config.cache' or simply `-C') that saves
the results of its tests to speed up reconfiguring.  Caching is
disabled by default to prevent problems with accidental use of stale
cache files.

   If you need to do unusual things to compile the package, please try
to figure out how `configure' could check whether to do them, and mail
diffs or instructions to the address given in the `README' so they can
be considered for the next release.  If you are using the cache, and at
some point `config.cache' contains results you don't want to keep, you
may remove or edit it.

   The file `configure.ac' (or `configure.in') is used to create
`configure' by a program called `autoconf'.  You need `configure.ac' if
you want to change it or regenerate `configure' using a newer version
of `autoconf'.  If you are building a development version from the
Github source, for example, use `./autogen.sh' to generate `configure'
and other necessary installation scripts.

The simplest way to compile this package is:

  1. `cd' to the directory containing the package's source code and type
     `./configure' to configure the package for your system.

     Running `configure' might take a while.  While running, it prints
     some messages telling which features it is checking for.

  2. Type `make' to compile the package.

  3. Optionally, type `make check' to run any self-tests that come with
     the package. Note that `make -j check' is not supported as some
     tests share infrastructure and cannot be run in parallel.

  4. Type `make install' to install the programs and any data files and
     documentation.

  5. You can remove the program binaries and object files from the
     source code directory by typing `make clean'.  To also remove the
     files that `configure' created (so you can compile the package for
     a different kind of computer), type `make distclean'.  There is
     also a `make maintainer-clean' target, but that is intended mainly
     for the package's developers.  If you use it, you may have to get
     all sorts of other programs in order to regenerate files that came
     with the distribution.

  6. Often, you can also type `make uninstall' to remove the installed
     files again.

这里我就不翻译了,基本是按照安装步骤写的:

  1. ./autogen.sh
    第一步先执行这个脚本,执行过程会报错
autogen.sh: error: could not find libtool.  libtool is required to run autogen.sh.

有的机器如果装的了话,就不会报错,我这里没装这个库就会报错,所以需要安装这个库。

apt-get install autoconf automake libtool m4 pkg-config

Ubuntu比较麻烦,要装这么多,如果是centos的话,就装一个libtool就可以。

然后执行脚本,这次就成功了。

  1. ./configure
    基本每个开源项目都需要配置一下,才会生成makefile,这个配置其实有一些选项的,这些选项我这里就不说了,因为我也没研究。但是在上面文档中,我看到没有带参数也可以,所以就不需要去研究那些参数了。
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/libzmq.pc
config.status: creating doc/Makefile
config.status: creating builds/Makefile
config.status: creating builds/deprecated-msvc/Makefile
config.status: creating src/platform.hpp
config.status: executing depfiles commands
config.status: executing libtool commands
  1. make
    makefile已经生成了,就可以make了,make的过程比较顺利,不会出现啥问题的。

  2. make install
    编译完成之后,就是安装,就是把lib库和头文件,拷贝都对应的文件里面,我们在配置的时候使用了默认配置,所以这时候会安装到默认的文件夹中。
    lib库目录:

root@debian:/usr/local/include# ls /usr/local/lib/
libzmq.a  libzmq.la  libzmq.so	libzmq.so.5  libzmq.so.5.2.3  pkgconfig  python2.7  python3.5

头文件目录:

root@debian:/usr/local/include# ls /usr/local/include/
zmq.h  zmq_utils.h

自此安装成功。

22.2 简单使用

zmq api接口,这是网址是zmq api接口网页,不知道为什么看着真奇怪,不过先不管了,先写一个简单的程序使用一下吧。

22.2.1 客户端程序

#include <iostream>
#include <string.h>
#include "zmq.hpp"

using namespace std;

/**
    * @brief  Main构造函数
    * @param  
    * @retval 
    */ 
int main(int argc, char *argv)
{
    zmq::context_t context (1);
    zmq::socket_t socket(context, ZMQ_REQ);
    char buff[1024] = "Hello, world";

    socket.connect("tcp://122.51.111.216:5555");

    socket.send(buff, strlen(buff));
    //socket.recv(buff, 1024);

    socket.close();
    context.close();

    return 0;
}

是不是看着感觉很简单,都不用直接创建socket了,这就是封装的好处,不过我们还是需要看看底层的实现,不过不是现在,以后有空可以分析分析。

22.2.2 服务端程序

#include <iostream>
#include <string.h>
#include "zmq.hpp"
#include "stdio.h"


using namespace std;

/**
    * @brief  Main构造函数
    * @param  
    * @retval 
    */ 
int main(int argc, char *argv)
{
    zmq::context_t context (1);
    zmq::socket_t socket(context, ZMQ_REP);
    char buff[1024] = {0};

    socket.bind("tcp://*:5555");

    // while(1) {
        socket.recv(buff, 1024);
        printf("buff %s\n", buff);
    // }
   
    socket.close();
    context.close();

    return 0;
}

记得这个客户端的程度的类型是ZMQ_REQ,然而服务器的程序是ZMQ_REP,不要搞错了,要不然一直会报错,刚刚就是不知道,所以一直觉得为什么报错,好奇怪。

22.2.3 message客户端程序

#include <iostream>
#include <string.h>
#include "zmq.hpp"


using namespace std;
// export LD_LIBRARY_PATH="&LD_LIBRARY_PATH:/usr/local/lib"
/**
    * @brief  Main构造函数
    * @param  
    * @retval 
    */ 
int main(int argc, char *argv)
{
    zmq::context_t context (1);
    zmq::socket_t socket(context, ZMQ_REQ);
    char buff[1024] = "Hello, world";

    socket.connect("tcp://122.51.111.216:5555");

    zmq::message_t mess(buff, strlen(buff));
    socket.send(mess);
    //socket.send(buff, strlen(buff));
    //socket.recv(buff, 1024);

    socket.close();
    context.close();

    return 0;
}

zmq 也是可以用message的方式发送,所以我这里也尝试一下,结果发现确实挺方便的。

22.2.4 message服务器程序

#include <iostream>
#include <string.h>
#include "zmq.hpp"
#include "stdio.h"

// export LD_LIBRARY_PATH="&LD_LIBRARY_PATH:/usr/local/lib"
using namespace std;

/**
    * @brief  Main构造函数
    * @param  
    * @retval 
    */ 
int main(int argc, char *argv)
{
    zmq::context_t context (1);
    zmq::socket_t socket(context, ZMQ_REP);
    char buff[1024] = {0};

    socket.bind("tcp://*:5555");

    // while(1) {
        //socket.recv(buff, 1024);
        //printf("buff %s\n", buff);
    // }
    void *data = NULL;
    zmq::message_t mess;
    socket.recv(&mess);

    data = zmq_msg_data(mess.handle());
    printf("接收到的数据 %s\n", (char *)data);


    socket.close();
    context.close();

    return 0;
}

22.3 其他笔记

zmq_msg_more - indicate if there are more message parts to receive
这个是处理多帧消息的,如果是发送的比较大的包,可以使用这个接口来接收。

zmq_setsockopt - set 0MQ socket options
设置zmq的各种选项的。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值