boost asio ssl async_write 给我发送的大多是乱码
我正在使用 boost::asio::ssl 将非安全套接字转换为 ssl
虽然非 ssl 一切正常,但 async_write 给我发送的大多是乱码。
有趣的是,当我在每个 async_write 之间放置 sleep(1) 时,我得到的数据大多是好的数据,但有一点乱码(类似于“??????@??”),
我发送数据的速度越快,乱码就越多。得到...我完全迷失在这个问题上!
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);
在 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())
);
}
这是当我不放置 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?├??
似乎它已被编码或其他...
我通过此命令行连接到服务器:
openssl s_client -ssl3 -connect 127.0.0.1:4242
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在交错写入。
您应该只在第一个完成后执行下一个
async_write
- 即在done_writing
处理程序中。这就是为什么当你拨打更多电话时你会收到垃圾的原因......The problem is that you are interleaving writes..
You should only do the next
async_write
, once the first one completes - i.e. in thedone_writing
handler. This is why you get crap as you make more calls...