Open ShuiYunXi opened 5 months ago
It's really hard to tell what's happening here, with pieces of code missing and others formatted in lots of different styles.
It might be helpful if you can reduce this down to a minimal example that fails, posted in a text code block. This should be pretty small, since it seems to be failing right on the client construction.
But the error from the console is pretty clear. The library is claiming that you're giving it a string that is not UTF-8. Are you using Unicode or wide char strings? Sorry I don't know much about Windows and MSVS, and how it is configured.
ok,I built a simplest case. The function of this case is to create a client. I tried to let it print some messages to help me analyze the cause of the problem, but still failed. The code is as follows:
#include <iostream>
#include <string>
#include "mqtt/async_client.h"
const std::string SERVER_ADDRESS("tcp://broker.emqx.io:1883");
const std::string CLIENT_ID("example_client");
int main() {
try {
std::cout << "Creating MQTT client..." << std::endl;
mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID);
std::cout << "MQTT client created successfully." << std::endl;
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
std::cout << "Connecting to the MQTT server..." << std::flush;
auto connToken = cli.connect(connOpts);
connToken->wait();
std::cout << "Connected!" << std::endl;
std::cout << "Disconnecting from the MQTT server..." << std::flush;
cli.disconnect()->wait();
std::cout << "Disconnected!" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << "MQTT exception: " << exc.what() << std::endl;
return 1;
}
catch (const std::exception& exc) {
std::cerr << "Standard exception: " << exc.what() << std::endl;
return 1;
}
catch (...) {
std::cerr << "Unknown exception occurred." << std::endl;
return 1;
}
return 0;
}
The program running results are as follows: Note that I have successfully added and built the paho-mqtt3as.lib and paho-mqttpp3.lib libraries to my project, and I can run the server using the paho-mqtt-c version. But this exception still exists. The C++ library version I use is 1.4.0, and the C library version is: 1.3.13. I am not sure if there is any conflict between the two libraries. I hope to get your help. Thank you.
OK,I have been Sovled the fatal error! this is a Bug in 1.4.0version.Because I rolled back the code to 1.3.2, the previous version, and rebuilt from the source, the issue was resolved. My code as follow:
#include <iostream>
#include <string>
#include "mqtt/async_client.h"
const std::string SERVER_ADDRESS("tcp://broker.emqx.io:1883");
const std::string CLIENT_ID("example_client");
int main() {
try {
std::cout << "Creating MQTT client..." << std::endl;
mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID);
std::cout << "MQTT client created successfully." << std::endl;
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
std::cout << "Connecting to the MQTT server..." << std::flush;
auto connToken = cli.connect(connOpts);
connToken->wait();
std::cout << "Connected!" << std::endl;
std::cout << "Disconnecting from the MQTT server..." << std::flush;
cli.disconnect()->wait();
std::cout << "Disconnected!" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << "MQTT exception: " << exc.what() << std::endl;
return 1;
}
catch (const std::exception& exc) {
std::cerr << "Standard exception: " << exc.what() << std::endl;
return 1;
}
catch (...) {
std::cerr << "Unknown exception occurred." << std::endl;
return 1;
}
return 0;
}
and it's run of result as follow:
Hopefully this information gives you some ideas on how to fix this BUG. Thank you very much for your help
I used had compiled the paho-maqtt-cpp lib with OpenSSL,I can not use the lib to test on vs2022!! this is info: code:
fatal error image:
server test image: install library info :
I tried a number of ways, including recompiling the library and using official routines, but the mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID); This code error, or memory allocation error, or throw exception prompt string error, please help me solve, thank you