0

我有一个使用 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();
4

1 回答 1

0

尽管async_receive安排了在代码调用之后执行的读取操作run(),但在您调用async_receive函数本身时它仍然需要一个连接的套接字。这是因为在内部,async_receive实现如下:

this->get_service().async_receive(this->get_implementation(),buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));

该表达式返回当时的内部套接字句柄this->get_implementation()副本,在您的情况下未连接。

换句话说,在异步读取被安排之后,它不会帮助连接套接字,因为读取将被安排在一个未连接的套接字上。

于 2013-03-22T09:44:28.533 回答