boost asio ssl async_write 给我发送的大多是乱码

发布于 2024-11-18 07:35:19 字数 2170 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-11-25 07:35:19

问题是您正在交错写入。

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())
                          );
}

您应该只在第一个完成后执行下一个 async_write - 即在 done_writing 处理程序中。这就是为什么当你拨打更多电话时你会收到垃圾的原因......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文