我正在使用 apollo 代理和 mosquitto C 客户端库。成功发布消息后,我使用 mosquitto_disconnect(mosq) 函数断开客户端。但看起来客户端用来连接代理的端口仍然不是空闲的。有什么方法可以手动释放客户端用于连接 ac 程序中代理的端口?
void on_publish(struct mosquitto *mosq, void *userdata, int mid)
{
mosquitto_disconnect(mosq);
log_print("mqtt",1,"Inside publish callback");
}
int sendData_mqtt(char *address, char *port, char* url, char *data)
{
struct mosquitto *mosq;
int err;
char *clean_session = "true";
char *retain_flag = "true";
int port1= atoi(port);
mosq = mosquitto_new("unknown", clean_session, NULL);
mosquitto_publish_callback_set(mosq, on_publish);
mosquitto_username_pw_set(mosq,"admin","password");
err=mosquitto_connect(mosq, address,port1,60);
if(err != 0 ){
log_print("mqtt",1,"connect error= %s\n",mosquitto_strerror(err));
return -1;
}
else
log_print("mqtt",1,"successfully connected");
err = mosquitto_publish(mosq, NULL, "subject", strlen(data), data, 1, retain_flag);
sleep(5);
if(err != 0 ) {
log_print("mqtt",1,"Publish error = %s\n",mosquitto_strerror(err));
return -1;
}
mosquitto_loop_forever(mosq,-1,1);
return 10;
}