客户端无法接收数据 - 客户端中的socket.async_receive异常

时间:2013-03-22 08:15:30

标签: c++ boost-asio

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

1 个答案:

答案 0 :(得分: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()表达式返回当时的内部套接字句柄的副本,在您的情况下是未连接的。

换句话说,在安排了异步读取之后,它将无法帮助连接套接字,因为读取将在未连接的套接字上进行调度。