MQTT在linux下环境下的编译与配置

         安装的方法参照http://blog.youkuaiyun.com/Netown_Ethereal/article/details/22653125

做MQTT的开发,记录下一些学习过程。

安装包括:

1、RPM安装

2、源码安装


(一)使用RPM安装

安装环境Centos6.4

从下面的链接下载mosquitto的RPM安装包:点击打开链接(http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/i686/)


运行安装命令:

[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost xxx]# rpm -i mosquitto-1.3.1-3.1.i686.rpm   
  2. warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY</span>  

提示有warning,查了一下,这个warning是可以忽略的。方法是安下面的指令来安装:


[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost XXX]# rpm -Uhv mosquitto-1.3.1-3.1.i686.rpm   
  2. warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY  
  3. Preparing...                ########################################### [100%]  
  4.     package mosquitto-1.3.1-3.1.i686 is already installed</span>  

如上所示,显示安装成功。

下面查看一下服务。

[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost /]# mosquitto  
  2. mosquitto         mosquitto_passwd  </span>  

在命令行输入:mosquitto,按两次Tab键,出现了下面两条指令。表示mosquitto已经安装成功。启动mosquitto服务的话只需要输入mosquitto指令即可。如下:
[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">[root@localhost /]# mosquitto  
  2. 1396234681: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396234681: Using default config.  
  4. 1396234681: Opening ipv4 listen socket on port 1883.  
  5. 1396234681: Opening ipv6 listen socket on port 1883.  
  6. 1396234750: New connection from 192.168.139.80 on port 1883.  
  7. 1396234750: New client connected from 192.168.139.80 as qylMQTT (c1, k60).</span>  

最后一行qylMQTT是我在windows端开发的client id,表示已经从客户端连接到MQTT服务器了。


上面发现没有mosquitto的发布和订阅客户端,只有服务器。下面安装mosquitto的客户端。

1、从上面给的下载RPM包的地址下载mosquitto的客户端。

主要是以下几个RPM包:

libmosquitto1-1.3.1-3.1.i686.rpm   
mosquitto-clients-1.3.1-3.1.i686.rpm  

注意里面有好几个是c++的devel,不要下载错了,而且好像c++的devel也需要先安装 libmosquitto1-1.3.1-3.1.i686.rpm

2、按照上面提到的安装RPM包的方式安装上述两个RPM包

3、安装成功之后在命令行输入mosquitto,Tab两次,显示的命令如下:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub   


可见,已经安装上了client。


4、运行

打开服务器:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. 1396235903: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396235903: Using default config.  
  4. 1396235903: Opening ipv4 listen socket on port 1883.  
  5. 1396235903: Opening ipv6 listen socket on port 1883.  
  6. 1396235917: New connection from ::1 on port 1883.  
  7. 1396235917: New client connected from ::1 as mosqsub/1915-localhost. (c1, k60).  
  8. 1396235953: New connection from ::1 on port 1883.  
  9. 1396235953: New client connected from ::1 as mosqpub/2397-localhost. (c1, k60).  


订阅:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_sub -t test -d  
  2. Client mosqsub/1915-localhost. sending CONNECT  
  3. Client mosqsub/1915-localhost. received CONNACK  
  4. Client mosqsub/1915-localhost. sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)  
  5. Client mosqsub/1915-localhost. received SUBACK  
  6. Subscribed (mid: 1): 0  
  7. Client mosqsub/1915-localhost. received PUBLISH (d0, q0, r0, m0, 'test', ... (11 bytes))  
  8. hello world  
  9. Client mosqsub/1915-localhost. sending PINGREQ  
  10. Client mosqsub/1915-localhost. received PINGRESP  
  11. Client mosqsub/1915-localhost. sending PINGREQ  
  12. Client mosqsub/1915-localhost. received PINGRESP  
  13. Client mosqsub/1915-localhost. sending PINGREQ  
  14. Client mosqsub/1915-localhost. received PINGRESP  
  15. Client mosqsub/1915-localhost. sending PINGREQ  
  16. Client mosqsub/1915-localhost. received PINGRESP  


发布:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_pub -t test -m "hello world" -d  
  2. Client mosqpub/2397-localhost. sending CONNECT  
  3. Client mosqpub/2397-localhost. received CONNACK  
  4. Client mosqpub/2397-localhost. sending PUBLISH (d0, q0, r0, m1, 'test', ... (11 bytes))  
  5. Client mosqpub/2397-localhost. sending DISCONNECT  


订阅一栏中显示的“hello world”即是所发布的消息。


下面是自己测试的vc++客户端发布消息的例程,先记下,回头再详细谈谈用vc++开发mosquitto客户端的方法。

服务器:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto  
  2. 1396245090: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting  
  3. 1396245090: Using default config.  
  4. 1396245090: Opening ipv4 listen socket on port 1883.  
  5. 1396245090: Opening ipv6 listen socket on port 1883.  
  6. 1396245096: New connection from ::1 on port 1883.  
  7. 1396245096: New client connected from ::1 as mosqsub/19451-localhost (c1, k60).  
  8. 1396245108: New connection from 192.168.139.80 on port 1883.  
  9. 1396245108: New client connected from 192.168.139.80 as qylMQTT (c1, k60).  
  10. 1396245110: Socket error on client qylMQTT, disconnecting.  
  11. 1396245189: New connection from 192.168.139.80 on port 1883.  
  12. 1396245189: New client connected from 192.168.139.80 as qylMQTT (c1, k60).  
  13. 1396245190: Socket error on client qylMQTT, disconnecting.  


订阅:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost ~]# mosquitto_sub -t test -d  
  2. Client mosqsub/19451-localhost sending CONNECT  
  3. Client mosqsub/19451-localhost received CONNACK  
  4. Client mosqsub/19451-localhost sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)  
  5. Client mosqsub/19451-localhost received SUBACK  
  6. Subscribed (mid: 1): 0  
  7. Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (4 bytes))  
  8. Hell  
  9. Client mosqsub/19451-localhost sending PINGREQ  
  10. Client mosqsub/19451-localhost received PINGRESP  
  11. Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (48 bytes))  
  12. Hello world, this is a message from vc++ client.  

接收端第一次只收到了“Hell”,大概是因为发送的时候计算payload的长度时出错了。第二次接受即正常。


(二)源码安装

事实上从去年到今天最开始的时候我都是使用源码安装的mosquitto,但是今天上午安装最新版的mosquitto时,发现源码安装不上,然后为了尽快测试我的vc++客户端,才选用的RPM安装。下午集中解决了一下源码安装的问题。


1、系统:centos6.4

2、下载源码:下载地址(http://mosquitto.org/download/)

我下载的是最新更新的mosquitto-1.3.1.tar.gz


3、解压缩

tar zxvf mosquitto-1.3.tar.gz 


4、暂时用不到SSL,所以安装的时候我把SSL关闭了

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost mosquitto-1.3]# make WITH_TLS=no  
  2. set -e; for d in lib client src; do make -C ${d}; done  
  3. make[1]: Entering directory `/qyl/mosquitto-1.3/lib'  
  4. cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_THREADING -DWITH_SRV -c mosquitto.c -o mosquitto.o  
  5. In file included from mosquitto.c:46:  
  6. mosquitto_internal.h:51:20: error: ares.h: No such file or directory  
  7. In file included from mosquitto.c:46:  
  8. mosquitto_internal.h:238: error: expected specifier-qualifier-list before ?.res_channel?  
  9. mosquitto.c: In function ?.osquitto_loop?.  
  10. mosquitto.c:834: error: ?.truct mosquitto?.has no member named ?.chan?  
  11. mosquitto.c:837: warning: implicit declaration of function ?.res_fds?  
  12. mosquitto.c:837: error: ?.truct mosquitto?.has no member named ?.chan?  
  13. mosquitto.c:917: error: ?.truct mosquitto?.has no member named ?.chan?  
  14. mosquitto.c:918: warning: implicit declaration of function ?.res_process?  
  15. mosquitto.c:918: error: ?.truct mosquitto?.has no member named ?.chan?  
  16. make[1]: *** [mosquitto.o] Error 1  
  17. make[1]: Leaving directory `/qyl/mosquitto-1.3/lib'  
  18. make: *** [mosquitto] Error 2  

在lib/mosquitto_internal.h里面找到ares.h,发现这个头文件是由宏定义WITH_SRV控制的。

不知道SRV SUPPORT是什么东西,但是查看mosquitto的更新日志,发现SRV support是今年3月16号才添加上的,我说去年源码安装一直没问题,今年怎么就不行了。

解决办法,在config.mk配置文件里面把srv support 关掉。如下图:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Build with SRV lookup support.  
  2. WITH_SRV:=no  

再执行make,就可以顺利安装了。


5、验证

在命令行输入mosquitto,Tab两次:

[plain]   view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@localhost mosquitto-1.3]# mosquitto  
  2. mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub   

mosquitto服务器和client都安装成功了。需要注意的是:它依赖于一个DNS解析库。可以通过yum -y install c-ares-devel指令来进行相应的安装。


c-ares-devel.i686 : Development files for c-ares
c-ares-devel.x86_64 : Development files for c-ares

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值