Hello,
I tried use the boost::asio to create a SSL-Server, but I fail.
The Code I used is attached to this Text.
acceptor_ and context_ give me true result, but the start_accept()
methode give me a Handshake-Error: 10009 (invalid descriptor handle).
How can I avoid this, and how can I fix the Code in the right point ?
Thanks for reading
paule32
// -----------------------------------------------------------------
// File: BoostServer.cc
// Author: (c) 2023 Jens Kallup - paule32
// All rights reserved
//
// only for education, and non-profit usage !
// -----------------------------------------------------------------
# include
(acceptor_.get_executor(), context_);
// ----------------------------------
// Starte den asynchronen Handshake
// ----------------------------------
ssl_socket_->async_handshake(ssl::stream_base::server,
[this](const boost::system::error_code& error) {
if (!error) {
// -----------------------------------
// Wenn der Handshake erfolgreich ist,
// akzeptiere die Verbindung
// -----------------------------------
if (ssl_socket_ && ssl_socket_->lowest_layer().is_open()) {
std::cout << "SSL-Handshake erfolgreich und verbunden."
<< std::endl;
acceptor_.async_accept(ssl_socket_->lowest_layer(),
[this](const boost::system::error_code& accept_error) {
if (!accept_error) {
handle_client();
} else {
std::cerr
<< "Accept error: "
<< accept_error.message()
<< std::endl;
}
});
} else {
std::cerr << "SSL-Handshake erfolgreich, aber
Verbindung nicht geöffnet."
<< std::endl;
}
} else {
std::cerr
<< "Handshake error: "
<< error.message()
<< " ("
<< error.value()
<< ")"
<< std::endl;
}
});
}
void handle_client() {
// ------------------------------------
// Lese/Schreibe Daten mit ssl_socket_
// ------------------------------------
boost::asio::async_read_until(*ssl_socket_, buffer_, '\n',
[this](const boost::system::error_code& read_error, std::size_t
bytes_transferred) {
if (!read_error) {
std::istream is(&buffer_);
std::string received_data;
std::getline(is, received_data);
std::cout << "Received data from client: "
<< received_data
<< std::endl;
// -----------------------------------------
// Hier kannst du auf die empfangenen Daten
// reagieren und die Antwort vorbereiten
// -----------------------------------------
std::string response = "Hello from the server!\n";
// -----------------------------------------
// Sende die Antwort an den Client
// -----------------------------------------
boost::asio::async_write(*ssl_socket_,
boost::asio::buffer(response),
[this](const boost::system::error_code& write_error,
std::size_t) {
if (!write_error) {
// -----------------------------
// Schließe die Verbindung
// -----------------------------
ssl_socket_->lowest_layer().close();
// -----------------------------
// Starte die nächste Akzeptanz
// -----------------------------
start_accept();
} else {
std::cerr
<< "Write error: "
<< write_error.message()
<< std::endl;
}
});
} else {
std::cerr
<< "Read error: "
<< read_error.message()
<< std::endl;
}
});
}
// ------------------------------------------------
// Überprüfung, ob der Acceptor initialisiert wurde
// ------------------------------------------------
bool isAcceptorInitialized() const {
return acceptor_.is_open();
}
// ------------------------------------------------
// Überprüfung, ob der Context initialisiert wurde
// ------------------------------------------------
bool isContextInitialized() {
try {
// ----------------------------------
// load certificate, and private key
// ----------------------------------
context_.use_certificate_chain_file("server.crt.pem");
context_.use_private_key_file("server.key.pem",
ssl::context::pem);
return true;
}
catch (const boost::system::system_error& e) {
handle_exception(e.what());
return false;
}
}
private:
ip::tcp::acceptor acceptor_;
ssl::context context_;
std::shared_ptr
participants (1)
-
Jens Kallup