溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MQTT協(xié)議C語言實現(xiàn)與Docker容器的集成

發(fā)布時間:2024-10-26 09:32:48 來源:億速云 閱讀:78 作者:小樊 欄目:編程語言

MQTT(Message Queuing Telemetry Transport)是一種輕量級的發(fā)布/訂閱消息傳輸協(xié)議,廣泛應(yīng)用于物聯(lián)網(wǎng)場景。下面是一個簡單的MQTT客戶端C語言實現(xiàn),并展示如何將其集成到Docker容器中。

1. MQTT客戶端C語言實現(xiàn)

首先,我們需要一個簡單的MQTT客戶端實現(xiàn)。這里我們使用mosquitto庫來實現(xiàn)。

安裝mosquitto

在Ubuntu上,可以使用以下命令安裝mosquitto庫:

sudo apt-get install libmosquitto-dev

MQTT客戶端代碼

創(chuàng)建一個名為mqtt_client.c的文件,內(nèi)容如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto.h>

#define Broker "tcp://broker.hivemq.com:1883"
#define ClientID "C_Client_1"

void on_connect(struct mosquitto *mosq, void *userdata, int rc) {
    printf("Connected with result code %d\n", rc);
    mosquitto_subscribe(mosq, NULL, "test/topic", 0);
}

void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *msg) {
    printf("Received message: %s from topic: %s\n", msg->payload, msg->topic);
}

void on_publish(struct mosquitto *mosq, void *userdata, int mid) {
    printf("Message %d published.\n", mid);
}

void on_subscribe(struct mosquitto *mosq, void *userdata, int mid, int granted_qos) {
    printf("Subscribed: %d\n", mid);
}

void on_disconnect(struct mosquitto *mosq, void *userdata, int rc) {
    printf("Disconnected.\n");
}

int main(int argc, char *argv[]) {
    struct mosquitto *mosq;
    int rc;

    mosquitto_lib_init();

    mosq = mosquitto_new(ClientID, true, NULL);
    if (!mosq) {
        fprintf(stderr, "Failed to create mosquitto client\n");
        return 1;
    }

    mosquitto_connect(mosq, Broker, 1883, 60);
    mosquitto_set_connect_callback(mosq, on_connect);
    mosquitto_set_message_callback(mosq, on_message);
    mosquitto_set_publish_callback(mosq, on_publish);
    mosquitto_set_subscribe_callback(mosq, on_subscribe);
    mosquitto_set_disconnect_callback(mosq, on_disconnect);

    rc = mosquitto_loop_start(mosq);
    if (rc != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "Failed to start loop\n");
        return 1;
    }

    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();

    return 0;
}

2. 編譯代碼

創(chuàng)建一個名為Makefile的文件,內(nèi)容如下:

CC = gcc
CFLAGS = -Wall -Wextra -o mqtt_client mqtt_client.c -lmosquitto

all: mqtt_client

mqtt_client:
	$(CC) $(CFLAGS)

clean:
	rm -f mqtt_client

運行以下命令編譯代碼:

make

3. Docker容器集成

創(chuàng)建一個名為Dockerfile的文件,內(nèi)容如下:

FROM gcc

COPY . /app
WORKDIR /app

RUN apt-get update && apt-get install -y libmosquitto-dev

RUN gcc -o mqtt_client mqtt_client.c -lmosquitto

CMD ["./mqtt_client"]

創(chuàng)建一個名為docker-compose.yml的文件,內(nèi)容如下:

version: '3'
services:
  mqtt_client:
    build: .
    ports:
      - "1883:1883"

運行以下命令構(gòu)建和啟動Docker容器:

docker-compose up --build

現(xiàn)在,你應(yīng)該能夠通過瀏覽器或其他MQTT客戶端連接到tcp://localhost:1883,并訂閱test/topic主題。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI