我有一个使用 boost asio 的简单版本的客户端。假设客户端在发送数据后会收到来自服务器的响应。这是客户端的代码
void RunClient()
{
try
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query( "127.0.0.1", boost::lexical_cast< std::string >( 7777 )); //9100
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::ip::tcp::socket socket(io_service);
socket.async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
boost::asio::connect(socket, endpoint_iterator);
boost::system::error_code ignored_error;
std::cout << "Sending message \n";
boost::asio::write(socket, boost::asio::buffer("Data to send"), ignored_error);
io_service.run();
}
catch (std::exception & ex)
{
std::cout << "[" << boost::this_thread::get_id()<< "] Exception: " << ex.what() << std::endl;
}
}
这是我的 ClientReceiveEvent
void ClientReceiveEvent(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if(!error)
{
std::cout << "Message: " << buf_client.data() << std::endl;
}
else
{
std::cout << "Error occurred." << error.message() << std::endl;
}
}
我从上述传入数据的方法中收到错误
Error occurred.The file handle supplied is not valid
有什么建议我在客户端做错了吗?
更新:
我得到了代码,但是我对为什么声明感到困惑
socket->async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
需要连接后放置。以及为什么声明
io_service->run();
需要放在最后。我认为这会启动异步过程。
我还想知道如何将数据重新发送到服务器。我可以成功发送一次。如何再次重新发送命令?
工作代码是:
boost::shared_ptr< boost::asio::io_service > io_service(new boost::asio::io_service);
boost::shared_ptr< boost::asio::ip::tcp::socket > socket( new boost::asio::ip::tcp::socket( *io_service ) );
boost::asio::ip::tcp::resolver resolver(*io_service);
boost::asio::ip::tcp::resolver::query query( "127.0.0.1", boost::lexical_cast< std::string >( 7777 )); //9100
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
socket->connect(endpoint_iterator->endpoint());
socket->async_receive(boost::asio::buffer(buf_client, 3000), 0, ClientReceiveEvent);
boost::system::error_code ignored_error;
std::cout << "Sending message \n";
boost::asio::write(*socket, boost::asio::buffer("some data"), ignored_error);
io_service->run();