2012-03-30 152 views
4

标题是我的问题。Boost Asio如何读取/写入不使用SSL的SSL套接字?

我已经发现了与此相关的话题在这里 - >Using SSL sockets and non-SSL sockets simultaneously in Boost.Asio?
,基本上我在同样的情况,但由于某种原因,我不能发表评论,有和/或咨询提问直接,所以我这样做作为一个新问题。

我有一个设置SSL插座ssl::stream<ip::tcp::socket> socket_;,在这里客户可以与

socket_.async_handshake(ssl::stream_base::server, session::handle_handshake) 

连接就好了,然后读/

async_write(socket_, buffer(send_data, send_length), session::handle_read) 
socket_.async_read_some(buffer(recieved_data, max_length), session::handle_read) 

但是现在写我想使用相同类型的插座建立不使用SSL的连接。所有的

首先我不确定该怎么办了“握手”,因为它与SSL连接正如我上面提到完成。
通过查看一些正常的boost.asio示例,我假设我不需要为非ssl连接执行此操作,并且只需在它被接受后直接读取/写入套接字?

然后作为下一步我试图做到这一点正如我上面提到的话题decsribed,我加了一个布尔SSL会话,以检查其是否SSL连接或没有。
然后,我使用socket_.lowest_layer()而不是在异步函数中使用socket_

这里的推荐代码:

if (sslEnabled) 
    boost::asio::async_write(secureSocket_); 
} else { 
    boost::asio::async_write(secureSocket_.lowest_layer()); 
} 

但是似乎编译犯规接受这个解决方案。当我尝试使用具有socket_.lowest_layer()作为流这个错误显示了一个异步函数编译代码(其只对async_read,ASYNC_WRITE也有类似的一个):

boost\asio\impl\read.hpp(263): error C2039: 'async_read_some' : is not a member of 'boost::asio::basic_socket<Protocol,SocketService>' 
      with 
      [ 
       Protocol=boost::asio::ip::tcp, 
       SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp> 
      ] 
      boost\asio\impl\read.hpp(255) : while compiling class template member function 'void boost::asio::detail::read_op<AsyncReadStream,MutableBufferSequence,CompletionCondition,ReadHandler>::operator()(const boost::system::error_code &,size_t,int)' 
      with 
      [ 
      AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>, 
       MutableBufferSequence=boost::asio::mutable_buffers_1, 
       CompletionCondition=boost::asio::detail::transfer_all_t, 
       ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>> 
      ] 
      boost\asio\impl\read.hpp(527) : see reference to class template instantiation 'boost::asio::detail::read_op<AsyncReadStream,MutableBufferSequence,CompletionCondition,ReadHandler>' being compiled 
      with 
      [ 
       AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>, 
       MutableBufferSequence=boost::asio::mutable_buffers_1, 
       CompletionCondition=boost::asio::detail::transfer_all_t, 
       ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>> 
      ] 
      c:\asio_test\source\server.cpp(131) : see reference to function template instantiation 'void boost::asio::async_read<boost::asio::basic_socket<Protocol,SocketService>,boost::asio::mutable_buffers_1,boost::_bi::bind_t<R,F,L>>(AsyncReadStream &,const MutableBufferSequence &,const ReadHandler &)' being compiled 
      with 
      [ 
       Protocol=boost::asio::ip::tcp, 
       SocketService=boost::asio::stream_socket_service<boost::asio::ip::tcp>, 
       R=void, 
       F=boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>, 
       L=boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>, 
       AsyncReadStream=boost::asio::basic_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp>>, 
       MutableBufferSequence=boost::asio::mutable_buffers_1, 
       ReadHandler=boost::_bi::bind_t<void,boost::_mfi::mf2<void,CSocket,const boost::system::error_code &,size_t>,boost::_bi::list3<boost::_bi::value<CSocket *>,boost::arg<1>,boost::arg<2>>> 
      ] 

Build FAILED. 

所以我现在几乎卡住了,我真的希望你能帮助我。搜索带来的误差高达不信邪,因为按理说应该有工作,我不知道这个错误可能是什么...

回答

8

其实我觉得我现在解决它。

1)非SSL的“握手”的确是没有必要的,我马上做async_reading在接受

2后),而不是async_read/write(socket_.lowest_layer(), ...)我不得不使用socket_.next_layer().async_read_some(buffer, handler)
async_write(socket_.next_layer(), ...)

我仍然不知道为什么它不会与socket_.lowest_layer()一起工作(如果有人知道,请解释),但至少它用上述方法工作得很好。我希望这会帮助其他人也有类似的问题;-)

相关问题