基于Mosquitto的MQTT C++服务器端示例代码:
时间: 2023-11-14 19:33:30 浏览: 368
Mosquitto本身是一个MQTT代理,而不是一个MQTT服务器。如果你需要一个MQTT服务器,可以考虑使用Eclipse Mosquitto的扩展版本Mosquitto.rsmb,它提供了一个MQTT服务器实现,可以用来实现自己的MQTT服务器。
以下是一个基于Mosquitto.rsmb的MQTT C++服务器端示例代码:
```cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <mosquitto.h>
#include <pthread.h>
#define MQTT_PORT 1883
#define MQTT_KEEP_ALIVE 60
#define MAX_CLIENTS 1024
static void on_connect(struct mosquitto *mosq, void *userdata, int rc) {
if (rc == 0) {
std::cout << "Connected to MQTT broker" << std::endl;
} else {
std::cerr << "Failed to connect to MQTT broker: " << mosquitto_connack_string(rc) << std::endl;
}
}
static void on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *msg) {
std::cout << "Received message on topic " << msg->topic << ": " << (char *)msg->payload << std::endl;
}
class Client {
public:
Client(int fd) : fd_(fd), mosq_(NULL) {}
~Client() {
if (mosq_) {
mosquitto_destroy(mosq_);
mosq_ = NULL;
}
if (fd_ > 0) {
close(fd_);
fd_ = -1;
}
}
int fd() const { return fd_; }
void start() {
mosq_ = mosquitto_new(NULL, true, this);
if (!mosq_) {
std::cerr << "Failed to create Mosquitto object" << std::endl;
return;
}
mosquitto_connect_callback_set(mosq_, on_connect);
mosquitto_message_callback_set(mosq_, on_message);
int rc = mosquitto_socket(mosq_);
if (rc == MOSQ_ERR_SUCCESS) {
rc = mosquitto_loop_start(mosq_);
}
if (rc != MOSQ_ERR_SUCCESS) {
std::cerr << "Failed to start Mosquitto loop: " << mosquitto_strerror(rc) << std::endl;
return;
}
char buf[1024];
while (true) {
int n = recv(fd_, buf, sizeof(buf), 0);
if (n <= 0) {
break;
}
mosquitto_raw_publish(mosq_, NULL, "test_topic", n, buf, 0, false);
}
}
private:
int fd_;
struct mosquitto *mosq_;
};
void *client_handler(void *arg) {
Client *client = (Client *)arg;
client->start();
delete client;
return NULL;
}
int main(int argc, char **argv) {
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) {
perror("Failed to create socket");
return 1;
}
struct sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(MQTT_PORT);
int optval = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Failed to bind socket");
return 1;
}
if (listen(listen_fd, SOMAXCONN) < 0) {
perror("Failed to listen on socket");
return 1;
}
std::cout << "Listening on port " << MQTT_PORT << std::endl;
while (true) {
int conn_fd = accept(listen_fd, NULL, NULL);
if (conn_fd < 0) {
perror("Failed to accept connection");
continue;
}
std::cout << "Accepted connection from " << inet_ntoa(addr.sin_addr) << std::endl;
pthread_t tid;
pthread_create(&tid, NULL, client_handler, new Client(conn_fd));
pthread_detach(tid);
}
close(listen_fd);
return 0;
}
```
此代码使用Mosquitto.rsmb作为MQTT服务器,并在本地的1883端口监听连接。在接受到客户端连接之后,它会启动一个新线程来处理该客户端连接。在处理过程中,它会接收客户端的消息,并使用Mosquitto API将其发布到名为“test_topic”的主题上。
请注意,此代码仅供参考,并不是一个完整的MQTT服务器实现。在实际使用中,还需要考虑安全性、可靠性、性能等因素。
阅读全文