简体   繁体   English

如何使用Boost :: Asio发送std :: vector <char>?

[英]How do I send a std::vector<char> using Boost::Asio?

I've been trying to achieve that for the better part of the day, I'd honestly apreciate any help. 我一直在努力实现这一目标,我真诚地赞美任何帮助。 Both of my apps, the client and the server started throwing "vector subscript out of range" exceptions. 我的两个应用程序,客户端和服务器都开始抛出“向量下标超出范围”的异常。

How does one do this thing properly ? 如何正确地做这件事?


Still trying to figure this out, anyone? 还在试图解决这个问题,有人吗?

As far as I undestand I'm supposed to create a 据我所知,我应该创造一个

boost::asio::basic_stream_socket stream; And then call : 然后打电话:

stream.send(boost::asio::buffer(data)); ?

I suppose it's perfectly possible to do it asynchronously ? 我想完全可以异步做到这一点? What are the differences between : basic_stream_socket::send() vs. basic_stream_socket::write_some() vs. basic_stream_socket::async_write_some() ? 有什么区别: basic_stream_socket::send()basic_stream_socket::write_some()basic_stream_socket::async_write_some()

basic_stream_socket::receive() vs. baic_stream_socket::read_some() vs. basic_stream_socket::async_read_some() ? basic_stream_socket::receive()baic_stream_socket::read_some()对比basic_stream_socket::async_read_some()

I'm assuming that send() & receive() are methods that I can call when I wish to make sure that 100% of data is sent/received - at the expense of blocking the socket? 我假设send()receive()是当我希望确保100%的数据被发送/接收时我可以调用的方法 - 以阻塞套接字为代价?

I'm assuming that write_some() & read_some() are methods that I can call when I'm not sure if 100% of data is sent/received - while still blocking the socket ? 我假设write_some()read_some()是当我不确定100%的数据是否被发送/接收时我可以调用的方法 - 同时仍然阻塞套接字?

I'm assuming that async_read_some() & async_write_some() are methods that don't block the socket and they read/write whatever they can ? 我假设async_read_some()async_write_some()是不阻塞套接字的方法,他们可以读/写任何东西?

Assuming you created a stream and are trying to send the vector<char> data : 假设您创建了一个并尝试发送vector<char> 数据

stream.send(boost::asio::buffer(data));

See boost:asio::buffer . 请参阅boost:asio :: buffer

I'm fairly new to boost asio too but here are my answers: 我也很擅长提升asio,但这是我的答案:

The functions with function names starting with async_ takes, amongst others, a callback function as argument which will be called when the operation finishes. 函数名以async_开头的函数除了其他之外还包含一个回调函数作为参数,该函数将在操作完成时调用。 That is, the function async_write_some() does not block but returns immediately and when the operation is finished, the callback function is invoked. 也就是说,函数async_write_some()不会阻塞但会立即返回,并且当操作完成时,将调用回调函数。

Your assumptions are correct, as far as I've understood. 据我所知,你的假设是正确的。 However, with the functions async_write_some etc. you have a callback function which takes the number of bytes transferred as argument. 但是,使用函数async_write_some等,您有一个回调函数,它将传输的字节数作为参数。

For example, here I have a function which reads from a socket: 例如,这里我有一个从套接字读取的函数:

boost::asio::async_read(
    *socket_,
    boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
    boost::bind(
        &sender::handle_read_content,
        this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred
    )
);

The callback function in there is handle_read_content, and it looks like this: 那里的回调函数是handle_read_content,它看起来像这样:

void sender::handle_read_content(
    const boost::system::error_code& err,
    std::size_t bytes_transferred
)

In the beginning I read this tutorial which was very good: Boost Asio 一开始我读了很好的教程: Boost Asio

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

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