windos下MQTT服务端和客户端的搭建

1、下载windos下服务器环境
emqttd-windows10-v2.2.0.zip

http://emqtt.com/docs/v2/install.html#windows

基本命令如下:

windos下MQTT服务端和客户端的搭建_第1张图片
1.png

解压压缩包 启动服务器,进入目录中打开服务器:(命令行下注意需要管理员权限)

windos下MQTT服务端和客户端的搭建_第2张图片
2.png

2、下载客户端测试
下载 org.eclipse.paho.ui.app-win32.win32.x86_64
http://download.csdn.net/detail/zhanyongli2008/9908108

打开客户端 打开程序设置IP地址,直接设置为本机地址:tcp://localhost:1883
打开两个客户端,都订阅一个主题,另外一个订阅主题的客户端会接收到

windos下MQTT服务端和客户端的搭建_第3张图片
3.png
windos下MQTT服务端和客户端的搭建_第4张图片
4.png

3、linux客户端连接
windos 服务器地址为:192.168.2.103

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "src/MQTTClient.h"
#include    
#include 
#include 

#define ADDRESS     "tcp://192.168.2.103:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

using namespace std;
int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;

    MQTTClient_message *receivemsg = NULL ;
    char* topicName_rev = NULL;
    int   topicLen_rev;

    int rc;
    int i;

    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 60;
    conn_opts.cleansession = 1;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(-1);
    }
    pubmsg.payload = (void *)PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_subscribe(client, "test", 1);                        /* 订阅一个客户端 的一个话题*/
    usleep(10000); 
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);  /* 发送消息  */
    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
    rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);
    for(;;)
    {
               if(MQTTClient_isConnected(client) == true)
               {
                   printf("alive \n");
               }else{
                   printf(" no alive \n");
                   break;
               }
            rc = MQTTClient_receive(client,&topicName_rev, &topicLen_rev, &receivemsg,5000);
               if(rc == MQTTCLIENT_SUCCESS)
               {
                printf("Message REv  %d delivered\n", rc);
                printf("topicName: %s  topicName_LEN: %d \n", topicName_rev,topicLen_rev);
                if(topicName_rev != NULL)
                {
                    printf("topicName: ");
                    for(i=0;ipayload,receivemsg->payloadlen,receivemsg->msgid);
                     if(strcmp((char *)receivemsg->payload,"ESC") == 0)
                    {
                        printf("ESC \n");
                        break;
                    }
                }
            }

            usleep(10000); 
            usleep(100000);
    }
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

windons客户端设置如下 需要订阅两个主题 MQTT Examples test

windos下MQTT服务端和客户端的搭建_第5张图片
5.png
windos下MQTT服务端和客户端的搭建_第6张图片
6.png

windos下MQTT服务端和客户端的搭建_第7张图片
7.png

Windows客户端
发送主题 test
内容发送 123后 linux客户端可以直接接收到
内容发送ESC后 linux客户端会直接关闭连接
windos下MQTT服务端和客户端的搭建_第8张图片
8.png

windos下MQTT服务端和客户端的搭建_第9张图片
9.png
windos下MQTT服务端和客户端的搭建_第10张图片
10.png

你可能感兴趣的:(windos下MQTT服务端和客户端的搭建)