views:

58

answers:

3

I am trying to make an asynchronised server in visual studio and I use

boost::asio::async_read(m_socket, boost::asio::buffer(m_buffer), 
                        boost::bind(&tcp_connection::handle_read, shared_from_this(),
                                    boost::asio::placeholders::error));

to get the buffer to be put in m_buffer

boost::array<char, 256> m_buffer;

but how do I get the size of this thing, m_buffer?

size() didn't work, end() didn't work.. Any help would be fantastic. Thanks in advance.

A: 

boost::arrays have constant size based on the second template argument. You can retrieve their size by calling their size() method. See boost::array documentation.

Jordan Lewis
+1  A: 

boost::array has a constant size. If you want to print it as a null-terminated string, use .data() to get a const char*.

If you just want to find the position of the \0, use std::find.

int size = find(array.begin(), array.end(), '\0') - array.begin();
KennyTM
std::cout.write(m_buffer.data(), std::find(m_buffer.begin(), m_buffer.end(), '\0') - m_buffer.begin()); still doesn't seem to output anything.
Anonymous
@Anonymous: Maybe the m_buffer is empty? Try to print `std::find(m_buffer.begin(), m_buffer.end(), '\0') - m_buffer.begin()`.
KennyTM
Nothing shows up. It returns nothing.
Anonymous
@Anon: It cannot "print nothing". Are you using `cout <<`?
KennyTM
std::cout << "We are reading: "; std::cout << find(m_buffer.begin(), m_buffer.end(), '\0') - m_buffer.begin();All I see is the first one.
Anonymous
@anon: `cout << (find(...) - m_buffer.begin());`
KennyTM
Ah, I fixed it, thanks for the answer.
Anonymous
A: 

If you specified the size of the buffer to be 256, and then you place a character at location 0 in the buffer and try to cout the buffer, it will print the entire buffer. This is because the program has no way of knowing that you placed only one "valid" character in the buffer. You will need to keep a separate "pointer" into the buffer yourself that lets you know where your data ends and the "empty" part of the buffer begins.

Polaris878