I'm in the process of converting my non secure socket to ssl, using boost::asio::ssl
While everything was working fine with non-ssl, async_write send me mostly gibberish.
The funny thing is when I put a sleep(1) between every async_write, I get mostly good data, with a little gibberish (something like that "?????@??" )
The faster I send data the most gibberish it gets... I'm completely lost on this problem !
initSSLConnection :
boost::asio::ssl::context *m_context = new boost::asio::ssl::context(m_acceptor.io_service(), boost::asio::ssl::context::sslv23);
m_context->set_options(boost::asio::ssl::context::default_workarounds
| boost::asio::ssl::context::no_sslv2
| boost::asio::ssl::context::single_dh_use);
m_context->set_password_callback(boost::bind(&Server::get_password, this));
m_context->use_certificate_chain_file("./ssl_key/cert.pem");
m_context->use_private_key_file("./ssl_key/key.pem", boost::asio::ssl::context::pem);
write on Socket :
while (1) {
boost::asio::async_write(m_socket,
boost::asio::buffer(ss.str().c_str(), ss.str().size()),
boost::bind(&MyClass::done_writing,
开发者_如何学编程 shared_from_this())
);
}
Here's what I get when I do not put sleep(1) :
`?D`?@?pR???c??_?@?pR??c?@?pR??␌?@???␌?D◆?P0AE0004J0??@?⎻RP0AE0004J0??@?XJ┘?D◆?P0AE0004J0??X?┬±?> ␋┌≤C5700␌??┬±??> ␋┌≤C5700␌?????┐?> ␋┌≤C5700␌?????????┬±??┐?
?┴?^> ␋┌≤C5700␌??
?┴?^^> ␋┌≤C5700␌?????
?┴?> ␋┌≤C5700␌?V
??@
?┴?> ␋┌≤C◆????␋┌≤F1DA0?│?ADO14F⎼␋???@?⎻R
? ␋┌≤D9A90?┌?┬±?
?┴?┬±?
?┴??┐?ADO14F⎼␋⎻??@?⎻R?
?
6A7BD600?≠??┌?◆????␋┌≤21ADC?├???
␉
◆????␋┌≤21ADC?├???
◆????␋┌≤21ADC?├??◆????␋┌≤21ADC?├??
It seems that it is encoded or something...
I connect to the server via this command line :
openssl s_client -ssl3 -connect 127.0.0.1:4242
The problem is that you are interleaving writes..
while (1) {
boost::asio::async_write(m_socket,
boost::asio::buffer(ss.str().c_str(), ss.str().size()),
boost::bind(&MyClass::done_writing,
shared_from_this())
);
}
You should only do the next async_write
, once the first one completes - i.e. in the done_writing
handler. This is why you get crap as you make more calls...
精彩评论