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