0

I tried using following code to read the number of available bytes in the socket (on the server side) and the variable packet_bytes does not do anything. I was expecting the number of bytes used by the packet to be read into packet_bytes but that doesn't seem to work.

std::size_t packet_bytes = 0;

socket_.async_read_some(boost::asio::buffer(data_, max_length),
            boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            packet_bytes));

I also tried std::size_t packet_bytes = socket_.available(); and that didn't return anything either. This is the entire code.

pandoragami
  • 5,387
  • 15
  • 68
  • 116

1 Answers1

3

The packet_bytes argument in the bind call should also be a placeholder:

socket_.async_read_some(boost::asio::buffer(data_, max_length),
    boost::bind(&session::handle_read, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));

Then in your handler function that argument will be the amount of bytes read.

See e.g. the example in the manual.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Isn't that parameter used for getting the bytes in the socket? – pandoragami Apr 05 '13 at 08:24
  • How do I create the handler function though? – pandoragami Apr 05 '13 at 08:27
  • @lost_with_coding Okay, after rereading your question, it seems you want to know the number of bytes that _can_ be read, i.e. the number of bytes in the receive buffer, and not the number of bytes you just received. Is that correct? – Some programmer dude Apr 05 '13 at 08:27
  • Exactly, how do I get the number of bytes for the packet received by the server? – pandoragami Apr 05 '13 at 08:28
  • No, the number of bytes received not the number of a bytes that can be read. Aren't they the same though. – pandoragami Apr 05 '13 at 08:31
  • basically the method similar to this `size_t length = sock->read_some(boost::asio::buffer(data), error);` – pandoragami Apr 05 '13 at 08:31
  • @lost_with_coding Then that's what my `async_read_some` does. The second argument to your handler will be the amount of data received. – Some programmer dude Apr 05 '13 at 08:32
  • But the manual you showed doesn't explain how to use the handler or even create one. I guess its an object? – pandoragami Apr 05 '13 at 08:34
  • @lost_with_coding If you look at the last example in the linked to manual page, it's exactly what you want. – Some programmer dude Apr 05 '13 at 08:36
  • Does the function `void my_class::read_handler` get called automatically when `async_read_some` is invoked ? – pandoragami Apr 05 '13 at 08:39
  • 1
    @lost_with_coding Yes it gets called automatically, but probably not when `async_read_some` is invoked, but when data has been received (which might be any time). – Some programmer dude Apr 05 '13 at 08:41
  • I tried running that function in the code and sent a packet but it didn't do anything. I pasted `void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred) { std::cout< – pandoragami Apr 05 '13 at 08:46
  • @lost_with_coding And you have the `io_service` running? – Some programmer dude Apr 05 '13 at 08:54
  • Yup just like in the code for the async server `session(boost::asio::io_service& io_service) : socket_(io_service)` – pandoragami Apr 05 '13 at 08:55
  • @lost_with_coding You do know that the `io_service` object contains the actual event loop that will call your async handlers? You should either call its [`poll`](http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/io_service/poll.html) function at regular intervals, or let it do it automatically with the [`run`](http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/io_service/run.html) function. – Some programmer dude Apr 05 '13 at 08:58
  • I tried placing `io_service.run();` into the constructor for the `session` class but that caused the server to hang? – pandoragami Apr 05 '13 at 09:05
  • The `run` function goes into an infinite loop polling and dispatching events. You should call it _after_ everything is set up. See how their [async server example](http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html) does it. – Some programmer dude Apr 05 '13 at 09:20
  • Right, it turns out I already have it running, I'm already using the asyn server example. Thats the code I pasted `void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred) { std::cout< – pandoragami Apr 05 '13 at 09:28