preface

Mosquitos: A brief introduction to MQTT and mosquitos, but just to share some concepts for MQTT and mosquitos introduction. Let’s share a simple example this time.

We use the comprehensive demo of the previous article to transform. The general demo works like this:

This uses sockets as the means of interprocess communication, and the demo is written on Windows and needs to include Windows-specific headers.

In this note we change the above comprehensive demo to:

We use MQTT as a means of inter-process communication for process testing under Linux.

First paste the code:

Json_print process source code

Json_print. C:

GCC cjson. c json_print. C -l.. / mosquito-lmosquito-o json_print - export LD_LIBRARY_PATH=.. /mosquitto/build/lib:$LD_LIBRARY_PATH - author: ZhengN - public number: Linux */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include ".. /mosquitto/lib/mosquitto.h"

#define  STU_NAME_LEN  32

/* Student structure */
typedef struct _Student
{
    char name[STU_NAME_LEN];  / / name
    int num;                  / / student id
    int c_score;              // C language score
}StudentDef, *pStudentDef;

/* Internal function declaration */
static StudentDef StudentData_Prepare(void);
static char *StudentsData_Packet(pStudentDef _Stu);
static void StudentData_Send(const char *_data);
static void MqttClientInit(void);
static void MqttClientClean(void);

bool clean_session = true;
struct mosquitto *mosq = NULL;

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: The main * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * parameters: Note: * * * * returns: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
int main(void)
{
    StudentDef stu = {0};
    char *stu_data = NULL;
    int stu_count = 0;
    int i = 0;

    /* MQTT client initialization */
    MqttClientInit();

    /* Total number of students required to register */
    printf("Please input number of student: ");
    scanf("%d", &stu_count);

    while (i++ < stu_count)
    {
        /* Prepare data */
        stu = StudentData_Prepare();

        /* JSON format data group package */
        stu_data = StudentsData_Packet(&stu);

        /* Send data */
        StudentData_Send(stu_data);
    }

    /* Reclaim operation */
    MqttClientClean();

    return 0;
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: StudentData_Prepare, Prepare group packages needed data * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * Parameter: ** Description: ** Returned: To obtain the data of * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static StudentDef StudentData_Prepare(void)
{
    char name[STU_NAME_LEN] = {0};
    int num = 0;
    int c_score = 0;
    StudentDef stu;

    / * * / name
    printf("Please input name: ");
    scanf("%s", name);
    if (strlen(name) < STU_NAME_LEN)
    {
        strncpy((char*)&stu.name, name, strlen(name)+1);
    }
    else
    {
        printf("The name is too long\n");
    }
    
    Student id / * * /
    printf("Please input num (0~100): ");
    scanf("%d", &num);
    stu.num = num;

    /* C language score */
    printf("Please input c_score (0~100): ");
    scanf("%d", &c_score);
    stu.c_score = c_score;

    return stu;
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: StudentsData_Packet, JSON data set package * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * Parameter: _Stu: group student JSON Data required for the data package ** JSON format string * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static char *StudentsData_Packet(pStudentDef _Stu)
{
    char *res_string = NULL;    / / the return value
    cJSON *name = NULL;         / / name
    cJSON *num = NULL;          / / student id
    cJSON *c_score = NULL;      // C language score

    /* Create a JSON object with {} expanded */
    cJSON *obj = cJSON_CreateObject();
    if (obj == NULL)
    {
        goto end;
    }

    /* Create the "name": "XXX" key pair */
    name = cJSON_CreateString(_Stu->name);
    if (name == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(obj, "name", name);

    /* Create the "num": 207 key-value pair */
    num = cJSON_CreateNumber(_Stu->num);
    if (name == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(obj, "num", num);
    
    /* Create "c_score": 95 key-value pairs */
    c_score = cJSON_CreateNumber(_Stu->c_score);
    if (name == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(obj, "c_score", c_score); 

    res_string = cJSON_Print(obj);          // Render in JSON format
    // res_string = cJSON_PrintUnformatted(obj); // Render as unformatted

    if (res_string == NULL)
    {
        fprintf(stderr."Failed to print monitor.\n");
    }

/* Delete (free) */
end:
    cJSON_Delete(obj);
    return res_string;
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: StudentData_Send, JSON format string data set package sent * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ** Parameters: _data: data to send ** Description: ** Returned: JSON format string * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void StudentData_Send(const char *_data)
{
    printf("%s: %s\n\n", __FUNCTION__, _data);

    /* Release the message */
    mosquitto_publish(mosq, NULL."test_topic".strlen(_data)+1, (const char*)_data, 0.0);
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: MqttClientInit, The MQTT client initialization * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * Parameter: void ** Description: ** Returned: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void MqttClientInit(void)
{
    /* libmosquitto library initialization */
    mosquitto_lib_init();

    /* Create mosquitto client */
    mosq = mosquitto_new(NULL, clean_session, NULL);
    if(NULL == mosq)
    {
        printf("Create mqtt client failed... \n");
        mosquitto_lib_cleanup();
        return;
    }

    /* Connect to the server */
    if(mosquitto_connect(mosq, "localhost".1883.60))
    {
        printf("Unable to connect... \n");
        return;
    }

    /* Network message processing thread */
    int loop = mosquitto_loop_start(mosq);
    if(loop ! = MOSQ_ERR_SUCCESS) {printf("mosquitto loop error\n");
        return; }}/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: MqttClientClean, The MQTT client cleanup operations * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * Parameter: void ** Description: ** Returned: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void MqttClientClean(void)
{
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();
}
Copy the code

Json_parse process source code

Json_parse. C:

GCC cjson. c json_parse.c -l.. / mosquito-lmosquito-o json_parse - export LD_LIBRARY_PATH=.. /mosquitto/build/lib:$LD_LIBRARY_PATH

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include ".. /mosquitto/lib/mosquitto.h"

#define  STU_NAME_LEN  32

/* Student structure */
typedef struct _Student
{
    char name[STU_NAME_LEN];  / / name
    int num;                  / / student id
    int c_score;              // C language score
}StudentDef, *pStudentDef;

/* Internal function declaration */
static void StudentsData_Parse(pStudentDef _Stu, const char *_JsonStudnetData);
static void PrintParseResult(const pStudentDef _Stu);
static void SaveParseResult(const pStudentDef _Stu);
static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message);
static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result);

/* Internal global variable */
static FILE *stu_fp = NULL;

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: The main * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * parameters: Note: * * * * returns: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
bool clean_session = true;
int main(void)
{         
    struct mosquitto *mosq = NULL;

    /* libmosquitto library initialization */
    mosquitto_lib_init();

    /* Create mosquitto client */
    mosq = mosquitto_new(NULL, clean_session, NULL);
    if(NULL == mosq)
    {
        printf("Create mqtt client failed... \n");
        mosquitto_lib_cleanup();
        return 1;
    }

    /* Bind the connection, message receive callback function */
    mosquitto_connect_callback_set(mosq, my_connect_callback);
    mosquitto_message_callback_set(mosq, my_message_callback);

    /* Connect to the server */
    if(mosquitto_connect(mosq, "localhost".1883.60))
    {
        printf("Unable to connect... \n");
        return 1;
    }

    /* Loop through network messages */
    mosquitto_loop_forever(mosq, - 1.1);

    /* Reclaim operation */
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();

    return 0;
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: StudentsData_Parse, JOSN format student final data parsing * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ** Parameter: _JsonStudnetData: JSON data _Stu: save parsed useful data ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void StudentsData_Parse(pStudentDef _Stu, const char *_JsonStudnetData)
{
    cJSON *student_json = NULL;   // student_json operates on an object that represents the expanded content of {}
    cJSON *name = NULL;             
    cJSON *num = NULL;
    cJSON *c_score = NULL;
    
    /* Start parsing */
    student_json = cJSON_Parse(_JsonStudnetData);
    if (NULL == student_json)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if(error_ptr ! =NULL)
        {
            fprintf(stderr."Error before: %s\n", error_ptr);
        }
        goto end;
    }

    /* Get the value of name */
    name = cJSON_GetObjectItemCaseSensitive(student_json, "name");
    if(cJSON_IsString(name) && (name->valuestring ! =NULL))
    {
        memset(&_Stu->name, 0, STU_NAME_LEN*sizeof(char));
        memcpy(&_Stu->name, name->valuestring, strlen(name->valuestring));
    }

    /* Get num */
    num = cJSON_GetObjectItemCaseSensitive(student_json, "num");
    if (cJSON_IsNumber(num))
    {
        _Stu->num = num->valueint;
    }

    /* Parse to get c_score */
    c_score = cJSON_GetObjectItemCaseSensitive(student_json, "c_score");
    if (cJSON_IsNumber(c_score))
    {
        _Stu->c_score = c_score->valueint;
    }

end:
    cJSON_Delete(student_json);
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: PrintParseResult, Print resolution result * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * parameters: Note: * * * * returns: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void PrintParseResult(const pStudentDef _Stu)
{
    printf("name: %s, num: %d, c_score: %d\n\n", _Stu->name, _Stu->num, _Stu->c_score);
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: SaveParseResult, Save the analytic results * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * * parameters: _Stu: data to save ** Description: ** Returned: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void SaveParseResult(const pStudentDef _Stu)
{
    char write_buf[512] = {0};
    static int stu_count = 0;

    /* Open the file in a way that appends the content to the end of the file */
	if((stu_fp = fopen("ParseResult.txt"."a+")) = =NULL)
	{
		printf("Open file error! \n");
		return exit(EXIT_FAILURE);
	} 

    /* Writes files in the specified format */
    snprintf(write_buf, 512."name: %s, num: %d, c_score: %d\n", _Stu->name, _Stu->num, _Stu->c_score);
    size_t len = fwrite((char*)write_buf, 1.strlen(write_buf), stu_fp);

    /* File location pointer offset */
    fseek(stu_fp, len * stu_count, SEEK_SET);
    stu_count++;

    /* Close the file */
    fclose(stu_fp);
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: my_message_callback, Message receiving callback function * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * * parameters: Returns: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
    StudentDef stu = {0};

    if(message->payloadlen)
    {
        printf("%s %s\n", message->topic, (char*)message->payload);

        /* Parse JSON data */
        StudentsData_Parse(&stu, (const char*)message->payload);  

        /* Prints the parsing result */
        PrintParseResult(&stu);  

        /* Save data to file */
        SaveParseResult(&stu); 
    }
    else
    {
        printf("%s (null)\n", message->topic);
    }
    fflush(stdout);
}

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * functions: my_connect_callback, Connect the callback function * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * * parameters: Returns: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
    if(! result) {/* Subscribe to the message for the test_topic topic */
        mosquitto_subscribe(mosq, NULL."test_topic".0);
    }
    else
    {
        fprintf(stderr."Connect failed\n"); }}Copy the code

Compile operation

Json_parse and jSON_print

gcc cJSON.c json_parse.c -L .. /mosquitto/build/lib -lmosquitto -o json_parse gcc cJSON.c json_print.c -L .. /mosquitto/build/lib -lmosquitto -o json_printCopy the code

The link dynamic library is used here to generate executable programs.

Json_parse and json_print

An error occurs when executing these two programs:

./json_parse: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
/json_print: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
Copy the code

This is because the shared library file libmosquito.so.1 could not be found and the load failed.

The libmosquito.so.1 library is not in the /usr/lib directory where Linux normally searches for libraries to use.

There are two solutions: one is to copy the file to /usr/lib, but this is not allowed and users are not allowed to copy things to this path. The other option is to add the libmosquito.so.1 library path to the dynamic library search path as follows:

export LD_LIBRARY_PATH=.. /mosquitto/build/lib:$LD_LIBRARY_PATHCopy the code

Add the dynamic library search path as described above to run both programs:

Parseresult.txt gets:

Successful experiment!

The above is the share of this time, the code is written in haste, if there is any mistake, please point out, thank you! Since it took a lot of time to prepare the demo, I also wrote a lot of comments. Mosquitto /client/pub_client.c, mosquitto/client/sub_client.c, mosquitto/client/sub_client.c

The demo of this article: