简体   繁体   English

使用boost :: asio创建一个异步UDP客户端,表达式:字符串迭代器不可取消

[英]making an asynchronous UDP client using boost::asio , Expression: string iterator not dereferencable

I am making an asynchronous UDP client using boost::asio 我正在使用boost :: asio制作异步UDP客户端

the send data is OK when receive data that async_receive_from is error 接收到async_receive_from错误的数据时,发送数据正常

error message: Expression: string iterator not able to de-reference. 错误消息:表达式:字符串迭代器无法取消引用。

What's wrong with my code ? 我的代码有什么问题?
Can anyone explain. 谁能解释。 Thanks for a lot. 非常感谢。

UDPClient::UDPClient()
    : socket_(io_service, udp::endpoint (udp::v4(), 0))
{

    receiver_endpoint = boost::asio::ip::udp::endpoint(
        boost::asio::ip::address::from_string("127.0.0.1"),
        8080);
    do_receive();
    boost::function0< void> f =  boost::bind(&UDPClient::Sendtest,this);
    boost::thread t(f);
    io_service.run();
}
void UDPClient::do_receive()
{
    socket_.async_receive_from(boost::asio::buffer(recv_buffer), receiver_endpoint,
        boost::bind(&UDPClient::handle_receive, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
}

void UDPClient::handle_receive(const boost::system::error_code& error, size_t bytes_transferred)
{
    std::cout << "recve" << std::endl;
    if (!error || error == boost::asio::error::message_size)
        do_receive();
}

void UDPClient::Sendtest()
{
    while(true)
    {
        boost::thread::sleep(boost::get_system_time()+boost::posix_time::seconds(10));  
        str = "23434";
        size_t leng = str.length();
        socket_.async_send_to( boost::asio::buffer(str,leng) ,
            receiver_endpoint,
            boost::bind(&UDPClient::handle_write,this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred
            )       
            );
    }
}

void UDPClient::handle_write(const boost::system::error_code &error,size_t bytes_transferred)
{
    cout << "handle_write_over! " << endl;
}
int main()
{
    UDPClient updclient;
}

These three lines look like the problem to me: 这三行对我来说似乎是个问题:

str = "23434";
size_t leng = str.length();
socket_.async_send_to( boost::asio::buffer(str,leng) ,

Assuming that str is declared as a std::string object, you need to know that std::string is not implicitly convertable to an ASIO buffer. 假设str被声明为std::string对象,则需要知道std::string不能隐式转换为ASIO缓冲区。

You might try this: 您可以尝试以下方法:

stocket_.async_send_to(boost::asio::buffer(&str.front(), leng),

Or this ought to compile correctly: 否则应该正确编译:

stocket_.async_send_to(boost::asio::buffer(str),

I would note additionally that you've got a different problem in this program, in that you're reusing the buffer before you are certain that the data has been sent. 我还要另外指出,您在该程序中遇到了另一个问题,即在确定已发送数据之前重用了缓冲区。 That's beyond the scope of this problem though, and represents a design issue you'll have to address on your own or in a different question. 但是,这超出了此问题的范围,它代表您必须独自或在其他问题中解决的设计问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM