您的位置:首页 > 产品设计 > UI/UE

MQTT学习之mosquitto安装

2014-03-31 11:03 393 查看
做MQTT的开发,记录下一些学习过程。

安装包括:

1、RPM安装

2、源码安装

(一)使用RPM安装

安装环境Centos6.4

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

运行安装命令:

[root@localhost xxx]# rpm -i mosquitto-1.3.1-3.1.i686.rpm
warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY


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

[root@localhost XXX]# rpm -Uhv mosquitto-1.3.1-3.1.i686.rpm
warning: mosquitto-1.3.1-3.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID 49e1d0b1: NOKEY
Preparing...                ########################################### [100%]
package mosquitto-1.3.1-3.1.i686 is already installed


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

下面查看一下服务。

[root@localhost /]# mosquitto
mosquitto         mosquitto_passwd


在命令行输入:mosquitto,按两次Tab键,出现了下面两条指令。表示mosquitto已经安装成功。启动mosquitto服务的话只需要输入mosquitto指令即可。如下:

[root@localhost /]# mosquitto
1396234681: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting
1396234681: Using default config.
1396234681: Opening ipv4 listen socket on port 1883.
1396234681: Opening ipv6 listen socket on port 1883.
1396234750: New connection from 192.168.139.80 on port 1883.
1396234750: New client connected from 192.168.139.80 as qylMQTT (c1, k60).


最后一行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两次,显示的命令如下:

[root@localhost ~]# mosquitto
mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub


可见,已经安装上了client。

4、运行

打开服务器:

[root@localhost ~]# mosquitto
1396235903: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting
1396235903: Using default config.
1396235903: Opening ipv4 listen socket on port 1883.
1396235903: Opening ipv6 listen socket on port 1883.
1396235917: New connection from ::1 on port 1883.
1396235917: New client connected from ::1 as mosqsub/1915-localhost. (c1, k60).
1396235953: New connection from ::1 on port 1883.
1396235953: New client connected from ::1 as mosqpub/2397-localhost. (c1, k60).


订阅:

[root@localhost ~]# mosquitto_sub -t test -d
Client mosqsub/1915-localhost. sending CONNECT
Client mosqsub/1915-localhost. received CONNACK
Client mosqsub/1915-localhost. sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)
Client mosqsub/1915-localhost. received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/1915-localhost. received PUBLISH (d0, q0, r0, m0, 'test', ... (11 bytes))
hello world
Client mosqsub/1915-localhost. sending PINGREQ
Client mosqsub/1915-localhost. received PINGRESP
Client mosqsub/1915-localhost. sending PINGREQ
Client mosqsub/1915-localhost. received PINGRESP
Client mosqsub/1915-localhost. sending PINGREQ
Client mosqsub/1915-localhost. received PINGRESP
Client mosqsub/1915-localhost. sending PINGREQ
Client mosqsub/1915-localhost. received PINGRESP


发布:

[root@localhost ~]# mosquitto_pub -t test -m "hello world" -d
Client mosqpub/2397-localhost. sending CONNECT
Client mosqpub/2397-localhost. received CONNACK
Client mosqpub/2397-localhost. sending PUBLISH (d0, q0, r0, m1, 'test', ... (11 bytes))
Client mosqpub/2397-localhost. sending DISCONNECT


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

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

服务器:

[root@localhost ~]# mosquitto
1396245090: mosquitto version 1.3.1 (build date 2014-03-25 00:14:12+0000) starting
1396245090: Using default config.
1396245090: Opening ipv4 listen socket on port 1883.
1396245090: Opening ipv6 listen socket on port 1883.
1396245096: New connection from ::1 on port 1883.
1396245096: New client connected from ::1 as mosqsub/19451-localhost (c1, k60).
1396245108: New connection from 192.168.139.80 on port 1883.
1396245108: New client connected from 192.168.139.80 as qylMQTT (c1, k60).
1396245110: Socket error on client qylMQTT, disconnecting.
1396245189: New connection from 192.168.139.80 on port 1883.
1396245189: New client connected from 192.168.139.80 as qylMQTT (c1, k60).
1396245190: Socket error on client qylMQTT, disconnecting.


订阅:

[root@localhost ~]# mosquitto_sub -t test -d
Client mosqsub/19451-localhost sending CONNECT
Client mosqsub/19451-localhost received CONNACK
Client mosqsub/19451-localhost sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)
Client mosqsub/19451-localhost received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (4 bytes))
Hell
Client mosqsub/19451-localhost sending PINGREQ
Client mosqsub/19451-localhost received PINGRESP
Client mosqsub/19451-localhost received PUBLISH (d0, q0, r0, m0, 'test', ... (48 bytes))
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关闭了

[root@localhost mosquitto-1.3]# make WITH_TLS=no
set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/qyl/mosquitto-1.3/lib'
cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_THREADING -DWITH_SRV -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:46:
mosquitto_internal.h:51:20: error: ares.h: No such file or directory
In file included from mosquitto.c:46:
mosquitto_internal.h:238: error: expected specifier-qualifier-list before ?.res_channel?
mosquitto.c: In function ?.osquitto_loop?.
mosquitto.c:834: error: ?.truct mosquitto?.has no member named ?.chan?
mosquitto.c:837: warning: implicit declaration of function ?.res_fds?
mosquitto.c:837: error: ?.truct mosquitto?.has no member named ?.chan?
mosquitto.c:917: error: ?.truct mosquitto?.has no member named ?.chan?
mosquitto.c:918: warning: implicit declaration of function ?.res_process?
mosquitto.c:918: error: ?.truct mosquitto?.has no member named ?.chan?
make[1]: *** [mosquitto.o] Error 1
make[1]: Leaving directory `/qyl/mosquitto-1.3/lib'
make: *** [mosquitto] Error 2


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

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

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

# Build with SRV lookup support.
WITH_SRV:=no


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

5、验证

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

[root@localhost mosquitto-1.3]# mosquitto
mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub


mosquitto服务器和client都安装成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: