MQTT学习代码

消息发布客户端代码mqtt_pub.c

#include 
#include 
#include 
#include 
 
#define HOST "localhost"
#define PORT  1883
#define KEEP_ALIVE 60
#define MSG_MAX_SIZE  512
 
bool session = true;
 
int main()
{
    char buff[MSG_MAX_SIZE];
    struct mosquitto *mosq = NULL;
    //libmosquitto 库初始化
    mosquitto_lib_init();
    //创建mosquitto客户端
    mosq = mosquitto_new(NULL,session,NULL);
    if(!mosq){
        printf("create client failed..\n");
        mosquitto_lib_cleanup();
        return 1;
    }
    //连接服务器
    if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
        fprintf(stderr, "Unable to connect.\n");
        return 1;
    }
    //开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
    int loop = mosquitto_loop_start(mosq);
    if(loop != MOSQ_ERR_SUCCESS)
    {
        printf("mosquitto loop error\n");
        return 1;
    }
    while(fgets(buff, MSG_MAX_SIZE, stdin) != NULL)
    {
        /*发布消息*/
        mosquitto_publish(mosq,NULL,"yxq",strlen(buff)+1,buff,0,0);
        memset(buff,0,sizeof(buff));
    }
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();
    return 0;
}

消息订阅客户端代码mqtt_sub.c

#include 
#include 
#include 

#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60

 struct mosquitto *mosq = NULL;

void connect_callback(struct mosquitto* mosq, void *userdata, int result)
{
    if(!result) {
        mosquitto_subscribe(mosq, NULL, "yxq", 2);
    }else{
        printf("connect failed.\n");
    }
}

void message_callback(struct mosquitto* mosq, void *userdata, const struct mosquitto_message *message){
    if(message->payloadlen) {
        printf("%s %s\n", message->topic, (char*)message->payload);
    }else{
        printf("%s null\n", message->topic);
    }
}

void subscribe_callback(){
    printf("subscribe mqtt topic successfully.\n");
}

int mqtt_init(){
    bool session = true;
    mosquitto_lib_init();
    mosq = mosquitto_new(NULL, session, NULL);
    if (!mosq) {
        printf("create server failed!\n");
        return -1;
    }
    mosquitto_connect_callback_set(mosq, connect_callback);
    mosquitto_message_callback_set(mosq, message_callback);
    mosquitto_subscribe_callback_set(mosq, subscribe_callback);

    while(1) {
        if (mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE) != MOSQ_ERR_SUCCESS){
            sleep(3);
        }else{
            break;
        }
    
    }
}

int main(){
    mqtt_init();

    //int ret = mosquitto_loop_start(mosq);
    //if(ret != MOSQ_ERR_SUCCESS)
    //{
    //    printf("mosquitto loop error.\n");
    //}
    int ret = mosquitto_loop_forever(mosq, -1, 1);

    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();

    return 0;
}

MQTT服务端运行

loh@ubuntu:~/Desktop/test/mqtt$ mosquitto -v
1692341823: mosquitto version 2.0.15 starting
1692341823: Using default config.
1692341823: Starting in local only mode. Connections will only be possible from clients running on this machine.
1692341823: Create a configuration file which defines a listener to allow remote access.
1692341823: For more details see https://mosquitto.org/documentation/authentication-methods/
1692341823: Opening ipv4 listen socket on port 1883.
1692341823: Opening ipv6 listen socket on port 1883.
1692341823: mosquitto version 2.0.15 running
1692341823: New connection from 127.0.0.1:38344 on port 1883.
1692341823: New client connected from 127.0.0.1:38344 as auto-A781A762-8C2D-8884-52DF-14BC17CD1E14 (p2, c1, k60).
1692341823: No will message specified.
1692341823: Sending CONNACK to auto-A781A762-8C2D-8884-52DF-14BC17CD1E14 (0, 0)
1692341823: Received SUBSCRIBE from auto-A781A762-8C2D-8884-52DF-14BC17CD1E14
1692341823: 	yxq (QoS 2)
1692341823: auto-A781A762-8C2D-8884-52DF-14BC17CD1E14 2 yxq
1692341823: Sending SUBACK to auto-A781A762-8C2D-8884-52DF-14BC17CD1E14

编译

gcc mqtt_pub.c -lmosquitto -o q

 

mosquitto开源库安装参考:

https://blog.csdn.net/kuniqiw/article/details/98962889

https://blog.csdn.net/qq_42998491/article/details/130795042

你可能感兴趣的:(网络编程,学习)