0
votes

I am making an asynchronous UDP client using boost::asio

the send data is OK when receive data that async_receive_from is error

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;
}
1
What is recv_buffer ?Blacktempel
i'm sorry forget the .h codedoube.w
boost::asio::io_service io_service; boost::asio::io_service::work io_work; udp::socket socket_; udp::endpoint receiver_endpoint; boost::array<char, 1024> recv_buffer; UDPClient(); void do_receive(); void Sendtest(); void handle_receive(const boost::system::error_code& error, size_t); void handle_write(const boost::system::error_code& error, size_t);doube.w
if i don't use the function that sendtest() the program is runing no errordoube.w
Please edit your question and add the declarations in there. Nobody wants to pick out each line from the comment section. Doesn't belong there.Blacktempel

1 Answers

0
votes

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.

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.