/*
 ============================================================================
 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. mxnet卷积神经网络训练MNIST数据集测试

    mxnet框架下超全手写字体识别—从数据预处理到网络的训练—模型及日志的保存 import numpy as np import mxnet as mx import logging logging. ...

  2. Wireshark 抓包小例子

    捕捉过滤器(CaptureFilters): 用于决定将什么样的信息记录在捕捉结果中. 语法详见:http://www.cnblogs.com/SZxiaochun/p/7920962.html 显示 ...

  3. linux中查看某个端口(port)

    查看 27017 端口: netstat -anp | grep 27017 Proto              Recv-Q   Send-Q   Local Address           ...

  4. SpringBoot系列二:搭建自己的第一个SpringBoot程序

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 一.根据官网手工搭建(http://projects.spring.io/spring-boot/#quick-start) 1 ...

  5. python3 post方式上传文件。

    借助第三方库:Requests 其官网地址:   http://python-requests.org       官网上写的安装方式:http://docs.python-requests.org/ ...

  6. Redis系列-php怎么通过redis扩展使用redis

    From: http://blog.csdn.net/love__coder/article/details/8691679 通过前面几篇blog,我们应该对redis有个大致的认识,这里再讲解下,p ...

  7. RGB转LAB色彩空间

    1.原理 RGB无法直接转换成LAB,需要先转换成XYZ再转换成LAB,即:RGB——XYZ——LAB 因此转换公式分两部分: (1)RGB转XYZ 假设r,g,b为像素三个通道,取值范围均为[0,2 ...

  8. android中YUV转RGB的方法

    在一个外国网站上看到一段YUV转RGB的程序很不错,根据维基上的知识,方法应该是没问题的,自己也用过了,效果没问题. 首先说一下android上preview中每一帧的信息都是YUV420的,或者叫N ...

  9. [转]Apache 监听端口失败,selinux惹的祸

    原文在此 CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小 ...

  10. angular.extend深拷贝(deep copy)

    在用到angular.extend的时候,正好碰到一个对象,是层层嵌套的Array, 结果发现只能extend第一层,查阅官文档,确实不支持deep copy: Note: Keep in mind ...