/*
 ============================================================================
 Name        : mqtest.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include <string.h>
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
    if(message->payloadlen){
        printf("%s %s\n", message->topic, message->payload);
    }else{
        printf("%s (null)\n", message->topic);

}

fflush(stdout);
}

void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
    int i;
    if(!result){

mosquitto_subscribe(mosq, NULL, "lgt", 2);
        /*mosquitto_publish(mosq,NULL,"lgt")*/
        /*int payloadlen, const void *payload, int qos, bool retain*/
    }else{
        fprintf(stderr, "Connect failed\n");
    }
}

void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
    int i;

printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
    for(i=1; i<qos_count; i++){
        printf(", %d", granted_qos[i]);
    }

printf("\n");
}

void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
    /* Pring all log messages regardless of level.*/
    printf("%s\n", str);
}

int main(void) {
    puts("!!!Hello World!!!");
/*    prints !!!Hello World!!! */

int i=0;
        char *host = "localhost";
        char dst[]="lgttest12321321";
        char dst1[20];
        char ddd[200];
        int port = 1883;
        int keepalive = 60;
        bool clean_session = true;
        struct mosquitto *mosq = NULL;
        mosquitto_lib_init();
        mosq = mosquitto_new("track01", clean_session, NULL);
            if(!mosq){
            fprintf(stderr, "Error: Out of memory.\n");
            return 1;
        }
            mosquitto_log_callback_set(mosq, my_log_callback);
        /*mosquitto_connect_callback_set(mosq, my_connect_callback);*/
        mosquitto_message_callback_set(mosq, my_message_callback);
        mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);

if(mosquitto_connect(mosq, host, port, keepalive)){
            fprintf(", %d", i);
            return 1;

}

for(;;)
                {

i++;
            /*itoa(i,dst1,10);*/
            sprintf(dst1, "%d", i);
            /**ddd=strcat(dst, dst1);*/
            //dst1=dst;
            mosquitto_publish(mosq, NULL, "lgt",strlen(dst1), dst1, 0, true);
            /*fprintf(", %d", i);*/
                }

mosquitto_loop_forever(mosq, -1, 1);

mosquitto_destroy(mosq);
        mosquitto_lib_cleanup();

return 0;
    /*return EXIT_SUCCESS;*/
}

MQTT-C-PUB的更多相关文章

  1. 云巴:基于MQTT协议的实时通信编程模型

    概要 有人常问,云巴实时通信系统到底提供了一种怎样的服务,与其他提供推送或 IM 服务的厂商有何本质区别.其实,从技术角度分析,云巴与其它同类厂商都是面向开发者的通信服务,宏观的编程模型都是大同小异, ...

  2. 基于MQTT协议进行应用开发

    官方协议有句如下的话来形容MQTT的设计思想: "It is designed for connections with remote locations where a "sma ...

  3. Mosquitto pub/sub服务实现代码浅析-主体框架

    Mosquitto 是一个IBM 开源pub/sub订阅发布协议 MQTT 的一个单机版实现(目前也只有单机版),MQTT主打轻便,比较适用于移动设备等上面,花费流量少,解析代价低.相对于XMPP等来 ...

  4. MQTT 简介

    MQTT 全称是 Message Queue Telemetry Transport,是一个轻量级的“发布/订阅”消息传输协议. 官网 http://mqtt.org/ 发布/订阅 MQTT 的基本概 ...

  5. emqtt 试用(三)mqtt 知识

    一.概念 MQTT 协议客户端库: https://github.com/mqtt/mqtt.github.io/wiki/libraries 例如,mosquitto_sub/pub 命令行发布订阅 ...

  6. kafka和mqtt的区别是什么?

    两者都是从传统的Pub/Sub消息系统演化出来的,但是进化方向不一样,比较如下: Kafka是为了数据集成的场景,与以往Pub/Sub消息总线不一样,通过分布式架构提供了海量消息处理.高容错的方式存储 ...

  7. 常见MQTT服务器搭建[转载]

    简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠的网络的进行远程传感器和控制设备通 ...

  8. 常见MQTT服务器搭建与试用

    常见MQTT服务器搭建与试用   简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,它比较适合于在低带宽.不可靠 ...

  9. MQTT研究之EMQ:【EMQ之HTTP认证/访问控制】

    今天进行验证的逻辑是EMQ的http的Auth以及ACL的逻辑. 首先,参照HTTP插件认证配置的说明文档进行基本的配置, 我的配置内容如下: ##-------------------------- ...

  10. MQTT研究之EMQ:【基础研究】

    EMQ版本V2, emqttd-centos7-v2.3.11-1.el7.centos.x86_64.rpm 下载地址:http://emqtt.com/downloads/2318/centos7 ...

随机推荐

  1. 机器学习 demo分西瓜

    周老师的书,对神经网络写了一个小的Demo 是最简单的神经网络,只有一层的隐藏层. 这次练习依旧是对西瓜的好坏进行预测. 主要分了以下几个步骤 1.数据预处理 对西瓜的不同特性进行数学编码表示(0~1 ...

  2. (笔记)如何安装Arm-linux-gcc

      如何安装Arm-linux-gcc   安装交叉编译工具链: 1.首先以root用户登入 2.复制arm-linux-gcc-4.3.2.tgz到根目录下tmp文件夹里 3.解压命令tar xvz ...

  3. windows下Nginx与tomcat组合简单使用

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中瓦片资源越来越多,如果提高瓦片的访问效率是一个需要解决的 ...

  4. result源码

    CREATE TABLE `result` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`thetime` CHAR(100) , `category ...

  5. appium的xpath定位问题及常用方法

    关于类似的帖子好像很多,但是没有找到具体能帮我解决问题的办法.还是自己深究了好久才基本知道app上面的xpath定位和web上的不同点: 先放一个图: A,先说说不用xpath的场景,一般是用于存在i ...

  6. 【RespberryPi】数码管

    http://blog.mangolovecarrot.net/2015/06/03/raspi-study0801/ 应该可以用两块74HC595来驱动显示8位数的数码管.

  7. git statsh命令报错解决

    git stash命令主要用于当在一个分支的开发工作未完成,却又要切换到另外一个分支进行开发的时候,除了commit原分支的代码改动的方法外,提供暂存代码的方式. git stash命令参考这篇:ht ...

  8. Vue.nextTick和Vue.$nextTick

    `Vue.nextTick(callback)`,当数据发生变化,更新后执行回调. `Vue.$nextTick(callback)`,当dom发生变化,更新后执行的回调. 参考原文:http://w ...

  9. JS貪食蛇網頁代碼

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <bo ...

  10. 视觉单词模型、词袋模型BoW

    多用于图像检索.分类 3.2.1.4 视觉单词模型 视觉词袋(BoVW,Bag of Visual Words)模型,是“词袋”(BoW,Bag of Words)模型从自然语言处理与分析领域向图像处 ...