1、下載windos下服務(wù)器環(huán)境
emqttd-windows10-v2.2.0.zip
http://emqtt.com/docs/v2/install.html#windows
基本命令如下:

1.png
解壓壓縮包 啟動(dòng)服務(wù)器,進(jìn)入目錄中打開服務(wù)器:(命令行下注意需要管理員權(quán)限)

2.png
2、下載客戶端測(cè)試
下載 org.eclipse.paho.ui.app-win32.win32.x86_64
http://download.csdn.net/detail/zhanyongli2008/9908108
打開客戶端 打開程序設(shè)置IP地址,直接設(shè)置為本機(jī)地址:tcp://localhost:1883
打開兩個(gè)客戶端,都訂閱一個(gè)主題,另外一個(gè)訂閱主題的客戶端會(huì)接收到

3.png

4.png
3、linux客戶端連接
windos 服務(wù)器地址為:192.168.2.103
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "src/MQTTClient.h"
#include <sys/time.h>
#include <termios.h>
#include <iostream>
#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); /* 訂閱一個(gè)客戶端 的一個(gè)話題*/
usleep(10000);
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); /* 發(fā)送消息 */
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;i<topicLen_rev;i++)
{
printf(" %c ", topicName_rev[i]);
}
printf("\n");
printf("Data: %s len:%d msgid: %d \n",(char *)receivemsg->payload,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客戶端設(shè)置如下 需要訂閱兩個(gè)主題 MQTT Examples test

5.png

6.png

7.png
Windows客戶端
發(fā)送主題 test
內(nèi)容發(fā)送 123后 linux客戶端可以直接接收到
內(nèi)容發(fā)送ESC后 linux客戶端會(huì)直接關(guān)閉連接

8.png

9.png

10.png