修改pubtomqtt的错误语句

This commit is contained in:
flykhan 2023-10-26 19:38:47 +08:00
parent e559f8926e
commit e2dcf2e20c
1 changed files with 15 additions and 9 deletions

View File

@ -4,14 +4,18 @@
#include <stdlib.h>
#include <string.h>
int isConnected = 0;
void connect_callback(struct mosquitto* mosq, void* obj, int rc) {
if (rc == 0) {
void connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
if (rc == 0)
{
printf("---连接服务器成功---\n");
isConnected = 1;
}
}
int main(int argc, char const* argv[]) {
if (argc < 3) {
int main(int argc, char const *argv[])
{
if (argc < 3)
{
printf("format: ./pub1 msgid topic msg");
return -1;
}
@ -21,12 +25,14 @@ int main(int argc, char const* argv[]) {
mosquitto_connect(client, "10.12.156.19", 1883, 60);
mosquitto_loop_start(client);
while (isConnected == 0)
;
int msg_id = atoi(argv[1]);
char *topic = (char *)argv[2];
char *msg = (char *)argv[3];
int flag =
mosquitto_publish(client, &msg_id, topic, strlen(msg), msg, 1, true);
if (flag == MOSQ_ERR_SUCCESS) {
if (flag == MOSQ_ERR_SUCCESS)
{
printf("---消息发布成功: %s(%d):%s---\n", topic, msg_id, msg);
mosquitto_destroy(client);
mosquitto_lib_cleanup();