智能家居设备状态监测与建议生成服务器
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/select.h> #include <arpa/inet.h> #include <sqlite3.h> #include <netinet/in.h>
#define MAX_BUFFER_SIZE 1024
typedef struct { int uid; char device_name[10]; char device_state[10]; char value[10]; char mode[10]; } Status;
typedef struct { int sockfd; struct sockaddr_in client_addr; socklen_t client_addr_len; sqlite3 *db; } ServerContext;
void handleClientRequest(ServerContext *context) { char buffer[MAX_BUFFER_SIZE]; int userid;
// 接收客户端发送的userid
ssize_t recvSize = recv(context->sockfd, &userid, sizeof(int), 0);
if (recvSize == -1) {
perror("userid接受失败\nrecv");
return;
} else if (recvSize == 0) {
perror("客户端已关闭连接");
return;
} else if (recvSize != sizeof(int)) {
perror("接收到的字节数不正确");
return;
}
userid = ntohl(userid); // 将网络字节序转换为主机字节序
printf("客户端已连接\n");
printf("userid:%d",userid);
// 查询数据库获取设备状态信息
char sql[100];
snprintf(sql, sizeof(sql), "SELECT * FROM Status WHERE uid = %d", userid);
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(context->db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "无法执行的语句: %s\n", sqlite3_errmsg(context->db));
return;
}
Status status;
memset(&status, 0, sizeof(Status)); // 清空status结构体
while (sqlite3_step(stmt) == SQLITE_ROW) {
status.uid = sqlite3_column_int(stmt, 1);
strncpy(status.device_name, sqlite3_column_text(stmt, 2), sizeof(status.device_name)-1);
strncpy(status.device_state, sqlite3_column_text(stmt, 3), sizeof(status.device_state)-1);
strncpy(status.value, sqlite3_column_text(stmt, 4), sizeof(status.value)-1);
strncpy(status.mode, sqlite3_column_text(stmt, 5), sizeof(status.mode)-1);
}
if (rc == SQLITE_DONE) {
fprintf(stderr, "No data found for user %d\n", userid);
sqlite3_finalize(stmt);
return; // 查询结果为空,直接返回
}
int columnCount = sqlite3_column_count(stmt);
printf("列数:%d\n", columnCount);
if (sqlite3_column_count(stmt) == 0) {
fprintf(stderr, "查询结果为空\n");
sqlite3_finalize(stmt);
return; // 查询结果为空,直接返回
}
sqlite3_finalize(stmt);
// 检查设备名称是否为空
if (strlen(status.device_name) == 0) {
perror("接收设备状态信息失败");
return;
}
// 分析设备状态并生成建议
char suggestion[MAX_BUFFER_SIZE];
if (strcmp(status.device_name, "空调") == 0) {
int temperature = atoi(status.value);
if (temperature < 24) {
snprintf(suggestion, sizeof(suggestion), "空调温度过低,建议提高温度至26℃");
} else {
snprintf(suggestion, sizeof(suggestion), "空调温度正常");
}
} else if (strcmp(status.device_name, "加湿器") == 0) {
int humidity = atoi(status.value);
if (humidity < 40 || humidity > 70) {
snprintf(suggestion, sizeof(suggestion), "加湿器湿度过高或过低,建议调整加湿器湿度");
} else {
snprintf(suggestion, sizeof(suggestion), "加湿器湿度正常");
}
} else {
snprintf(suggestion, sizeof(suggestion), "设备状态未知");
}
// 向客户端发送建议
ssize_t sendSize = send(context->sockfd, suggestion, strlen(suggestion), 0);
if (sendSize == -1) {
perror("发送失败\nsend");
return;
}
printf("%s",suggestion);
}
int main() { printf("正在连接中...\n"); int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("连接失败\nsocket"); return 1; }
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(12345);
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("连接失败\nbind");
close(sockfd);
return 1;
}
if (listen(sockfd, 5) == -1) {
perror("连接失败\nlisten");
close(sockfd);
return 1;
}
sqlite3 *db;
int rc = sqlite3_open("/mnt/g/Qt/Client/Smarthome_Client/database/database.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "数据库打开失败: %s\n", sqlite3_errmsg(db));
return 1;
}
ServerContext context;
context.sockfd = sockfd;
context.client_addr_len = sizeof(context.client_addr);
context.db = db;
fd_set readfds;
int maxfd = sockfd;
while (1) {
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
int activity = select(maxfd + 1, &readfds, NULL, NULL, NULL);
if (activity == -1) {
perror("select");
break;
}
if (FD_ISSET(sockfd, &readfds)) {
int clientSockfd = accept(sockfd, (struct sockaddr *)&context.client_addr, &context.client_addr_len);
if (clientSockfd == -1) {
perror("套接字接收失败\naccept");
break;
}
context.sockfd = clientSockfd;
handleClientRequest(&context);
close(clientSockfd);
}
}
sqlite3_close(db);
close(sockfd);
printf("服务器已关闭\n");
return 0;
}
下面是客户端的代码
#include "procession.h"
#include "ui_procession.h"
#include
Procession::Procession(int userid,QWidget *parent) : QWidget(parent), ui(new Ui::Procession), userid(userid) { ui->setupUi(this); processionWidget(); m_socket = new QTcpSocket(this); //connect(ui->connectBtn, &QPushButton::clicked, this, &Procession::on_connectBtn_clicked); connect(m_socket, &QTcpSocket::readyRead, this, &Procession::readyRead); connect(m_socket, &QTcpSocket::connected, this, &Procession::connected); connect(m_socket, &QTcpSocket::disconnected,this,&Procession::disconnected); connect(m_socket, QOverloadQAbstractSocket::SocketError::of(&QTcpSocket::error), this, &Procession::displayError); }
Procession::~Procession() { delete ui; }
void Procession::processionWidget() { setWindowTitle("服务器通信"); setAutoFillBackground(true); QPalette palette=this->palette(); QPixmap pixmap(":/user/image/image/net.jpg"); palette.setBrush(QPalette::Window, QBrush(pixmap)); setPalette(palette); setFixedSize(600,400); }
void Procession::connected()
{
ui->message->append("连接成功");
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << static_cast
void Procession::disconnected() { ui->message->append("连接失败"); m_socket->close(); }
void Procession::readyRead() { QByteArray data = m_socket->readAll(); if (data.isEmpty()) { ui->message->append("读取的数据为空"); return; } // 解析服务器返回的数据 QDataStream in(&data, QIODevice::ReadOnly); QString suggestion; in >> suggestion; // 使用重载的 >> 运算符来读取数据 if (in.status() != QDataStream::Ok) { ui->message->append("解析服务器返回的数据失败"); return; } ui->message->append(suggestion); m_socket->close(); }
void Procession::displayError(QAbstractSocket::SocketError error) { QString errorMessage; switch (error) { case QAbstractSocket::ConnectionRefusedError: errorMessage = "连接被拒绝"; break; case QAbstractSocket::RemoteHostClosedError: errorMessage = "远程主机关闭"; break; case QAbstractSocket::HostNotFoundError: errorMessage = "未找到主机"; break; case QAbstractSocket::SocketTimeoutError: errorMessage = "连接超时"; break; case QAbstractSocket::NetworkError: errorMessage = "网络错误"; break; default: errorMessage = "未知错误"; break; } ui->message->append("连接失败:" + errorMessage); }
void Procession::on_connectBtn_clicked() { if (m_socket->state() == QAbstractSocket::ConnectedState) { m_socket->disconnectFromHost(); // 先关闭套接字 qDebug() << "1"; }
ui->message->append("已经连接上服务器");
QString ip = ui->IP->text();
QString port = ui->port->text();
if (ip.isEmpty() || port.isEmpty()) {
ui->message->clear();
ui->message->append("请输入有效的IP地址和端口号");
return;
}
ui->message->clear();
ui->message->append("正在连接中...");
m_socket->connectToHost(ip, static_cast<quint16>(ui->port->text().toInt()));
}
#ifndef PROCESSION_H #define PROCESSION_H
#include
namespace Ui { class Procession; }
class Procession : public QWidget { Q_OBJECT
public: explicit Procession(int userid,QWidget *parent = nullptr); ~Procession(); void processionWidget();
private slots: //void on_pushButton_clicked(); void connected(); void disconnected(); void readyRead(); void displayError(QAbstractSocket::SocketError error);
void on_connectBtn_clicked();
private: Ui::Procession ui; int userid; QTcpSocket m_socket; };
#endif // PROCESSION_H
root@WIN-K3C9KG85CB8:/home/SmartHome/Server# valgrind --leak-check=full ./main.exec ==2651== Memcheck, a memory error detector ==2651== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==2651== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==2651== Command: ./main.exec ==2651== 正在连接中... 客户端已连接 ==2651== Invalid read of size 1 ==2651== at 0x484EFFF: strncpy (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==2651== by 0x10978C: handleClientRequest (in /home/SmartHome/Server/main.exec) ==2651== by 0x109D4F: main (in /home/SmartHome/Server/main.exec) ==2651== Address 0x0 is not stack'd, malloc'd or (recently) free'd ==2651== ==2651== ==2651== Process terminating with default action of signal 11 (SIGSEGV) ==2651== Access not within mapped region at address 0x0 ==2651== at 0x484EFFF: strncpy (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==2651== by 0x10978C: handleClientRequest (in /home/SmartHome/Server/main.exec) ==2651== by 0x109D4F: main (in /home/SmartHome/Server/main.exec) ==2651== If you believe this happened as a result of a stack ==2651== overflow in your program's main thread (unlikely but ==2651== possible), you can try to increase the size of the ==2651== main thread stack using the --main-stacksize= flag. ==2651== The main thread stack size used in this run was 8388608. ==2651== ==2651== HEAP SUMMARY: ==2651== in use at exit: 111,752 bytes in 220 blocks ==2651== total heap usage: 344 allocs, 124 frees, 142,600 bytes allocated ==2651== ==2651== LEAK SUMMARY: ==2651== definitely lost: 0 bytes in 0 blocks ==2651== indirectly lost: 0 bytes in 0 blocks ==2651== possibly lost: 0 bytes in 0 blocks ==2651== still reachable: 111,752 bytes in 220 blocks ==2651== of which reachable via heuristic: ==2651== length64 : 110,728 bytes in 219 blocks ==2651== suppressed: 0 bytes in 0 blocks ==2651== Reachable blocks (those to which a pointer was found) are not shown. ==2651== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==2651== ==2651== For lists of detected and suppressed errors, rerun with: -s ==2651== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) Segmentation fault 这是怎么回事,怎么改内容:根据Valgrind的输出,出现了一个无效的读取错误(Invalid read)和段错误(Segmentation fault)。错误发生在handleClientRequest函数中的strncpy函数调用处。可能的原因是在查询数据库获取设备状态信息时,没有正确处理查询结果为空的情况,导致status结构体的成员未被正确初始化。在使用strncpy函数拷贝字符串时,出现了对空指针的无效读取。
要解决这个问题,可以在查询结果为空时,提前返回或进行错误处理,以避免对未初始化的指针进行操作。另外,还可以检查其他地方是否存在类似的问题,确保所有使用字符串的地方都正确处理了空指针的情况。
原文地址: https://www.cveoy.top/t/topic/fCAl 著作权归作者所有。请勿转载和采集!