Answer the question
In order to leave comments, you need to log in
Blocking call to io_context.run() and socket.connect(). How to fix?
When you enter an incorrect ip-address or port number, the code module using boost::asio goes into the block permanently.
Simple code example:
int main(int argc, char* argv[]){
namespace ba = boost::asio;
using ba::ip::tcp;
ba::io_service service;
tcp::socket s(service);
if (argc<3)
return 255;
try {
auto it = tcp::resolver(service).resolve({argv[1], argv[2]});
s.connect(*it); // first resolved value
std::cout << "Connected " << s.local_endpoint() << " -> " << s.remote_endpoint() << "\n";
// demo write
ba::write(s, ba::buffer("hello world\n"));
} catch (const boost::system::system_error& e) {
std::cout << "ERROR:" << e.what() << "\n";
}}
s.connect(*it); // first resolved value
boost::asio::io_context io_context;
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::ip::tcp::resolver::results_type endpoints;
std::string address = ui->lineEdit_primaryServerAddress->text().toStdString();
endpoints = resolver.resolve(address, INET_SERVICE);
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.load_verify_file(SSL_CERT);
rb_baseClient c(io_context, ctx, endpoints);
c.set_request(CLNT_MSG_WHO_ARE_YOU);
try
{
boost::system::error_code _ec2;
io_context.run();
}
catch (std::exception& e)
{
io_context.stop();
std::cerr << e.what() << std::endl;
ui->label_primaryServerHostname->setStyleSheet("color: darkred");
this->pServerIsAlive = false;
emit pServerStatusChanged(false);
}
std::string reply=c.get_reply();
io_context.run();
rb_baseClient::rb_baseClient(boost::asio::io_context& io_context, boost::asio::ssl::context& context,
boost::asio::ip::tcp::resolver::results_type endpoints) :
_socket(io_context, context), _endpoints(endpoints)
{
// memset(_reply, 0, max_length);
// memset(_request, 0, max_length);
this->connect();
}
void rb_baseClient::connect()
{
try
{
_socket.set_verify_mode(boost::asio::ssl::verify_peer);
_socket.set_verify_callback(
boost::bind(&rb_baseClient::verify_certificate, this, _1, _2));
boost::asio::async_connect(_socket.lowest_layer(), _endpoints,
boost::bind(&rb_baseClient::handle_connect, this,
boost::asio::placeholders::error));
}
catch (std::exception& e)
{
cerr << "rb_baseClient::connect: " << e.what();
strcpy(_reply, SERVER_CONNECTION_FAILED);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question