|
Boost : |
From: Raffaele Romito (berserker_r_at_[hidden])
Date: 2007-12-07 03:33:07
Christopher Kohlhoff wrote:> What is the other end of the connection doing?> Can you send a small, complete program that shows the behaviour for me> to try. Thanks.
I'm using VC 8 and when I "pause" the debugger looking at the callstack of the two threads I can see that they are waiting on WSARecv.Here it is the code that show the problem: #define BUFFER_SIZE 1024*1024#define SERVER_PORT 1234 class MySocket{public: MySocket(boost::asio::io_service &service, boost::asio::ssl::context &context) : m_socket(service, context) { } bool connect(const std::string &ip, int port) { boost::asio::ip::tcp::resolver resolver(m_socket.get_io_service()); boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(ip), port)); boost::asio::ip::tcp::resolver::iterator endpoint_end; boost::system::error_code error = boost::asio::error::host_not_found; while(error && endpoint_iterator != endpoint_end) { m_socket.next_layer().close(); m_socket.next_layer().connect(*endpoint_iterator++, error); } return !error; } void handshake(bool client) { m_socket.handshake(client ? boost::asio::ssl::stream_base::client : boost::asio::ssl::stream_base::server); } bool read(void *data, size_t size) { return boost::asio::read(m_socket, boost::asio::buffer(data, size), boost::asio::transfer_all()) == size; } bool write(const void *data, size_t size) { return boost::asio::write(m_socket, boost::asio::buffer(data, size), boost::asio::transfer_all()) == size; } boost::asio::ssl::stream<boost::asio::ip::tcp::socket> m_socket;};void client_proc(){ try { boost::asio::io_service service; boost::asio::ssl::context ssl(service, boost::asio::ssl::context::sslv3_client); ssl.use_certificate_file("client.crt", boost::asio::ssl::context::pem); ssl.use_private_key_file("client.key", boost::asio::ssl::context::pem); MySocket socket(service, ssl); socket.connect("127.0.0.1", SERVER_PORT); socket.handshake(true); char *message = new char[BUFFER_SIZE]; memset(message, 'm', BUFFER_SIZE); socket.write(message, BUFFER_SIZE); delete message; } catch(std::exception &e) { OutputDebugString("client error: "); OutputDebugString(e.what()); OutputDebugString("\n"); }}void server_proc(){ try { boost::asio::io_service service; boost::asio::ssl::context ssl(service, boost::asio::ssl::context::sslv3_server); ssl.load_verify_file("ca.crt"); ssl.use_certificate_file("client.crt", boost::asio::ssl::context::pem); ssl.use_private_key_file("client.key", boost::asio::ssl::context::pem); MySocket socket(service, ssl); boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), SERVER_PORT)); acceptor.accept(socket.m_socket.next_layer()); socket.handshake(false); char *message = new char[BUFFER_SIZE]; memset(message, 0, BUFFER_SIZE); socket.read(message, BUFFER_SIZE); delete message; } catch(std::exception &e) { OutputDebugString("server error: "); OutputDebugString(e.what()); OutputDebugString("\n"); }} void main(){ boost::thread t1(&server_proc); boost::thread t2(&client_proc); t1.join(); t2.join();}
_________________________________________________________________
Scarica GRATIS la versione personalizzata MSN di Internet Explorer 7!
http://optimizedie7.msn.com/default.aspx?mkt=it-it
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk