Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:13:53+00:00 2026-06-04T21:13:53+00:00

I’m writing server with streaming protocol, so I need to do things like find

  • 0

I’m writing server with streaming protocol, so I need to do things like find end of header, copy it and then parse other stuff in boost buffer. As I found out the best way for manipulation with strings (find string in it, copy/delete using iterators and so on) is std::string. But I’m using char array buffer. So I’ll need to have two buffers – char array and std::string – every time I’ll need to manipulate with buffer I’ll need to convert char array to std::string, do my stuff and then convert it back to char array using std::string.c_str(). Another way I found is use streambuf (as I asked in my previous questition) and then create istream/ostream to it and fill content from it to std::string (as showed in documentation).
With streambuf I’ll need :
streambuf
mutable_buffers_type
istream
ostream
and std::string
But with use of char array and std::string I need just :
char array
std::string

So I think use of streambuf is waste of memory (I need to create buffer for every connection). Can I use std::string as boost buffer ? However I think there may be better way to do this. Can you give me an advice ?

EDIT:

I need to do something like this with my buffer, but char array don’t offer features like std::string (erase, substr, …) so I need to use std::string as buffer. What is the best way to use it as boost::buffer or what is the best way for parsing like this code ?

#include <iostream>

int main(int argc, char* argv[])
{

    //"header" is header
    //"end" is marking that this point is end of header
    //"data" is data after header
    //this all is sent in one packet which I receive to buffer
    //I need to fill "headerend" to std::string header and then remove "headerend" from begining of buffer
    //then continue parsing "data" which stay in buffer

    std::string buffer = "headerenddata"; //I receive something like this
    std::string header; //here I'll fill header (including mark of end of header)

    //find end of header and include also mark of end of header which is "end" (+3)
    int endOfHeader = int(buffer.find("end"))+3; 

    //fill header from buffer to string header
    header = buffer.substr(0, endOfHeader);

    //delete header from input buffer and keep data in it for next parsing
    buffer.erase(buffer.begin(), buffer.begin()+endOfHeader); 
    //will be just "data" becouse header and mark of header are removed
    std::cout << buffer << std::endl; 
    //will be "headerend" which is "header" and mark end of header which is "end"
    std::cout << header << std::endl;


    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:13:54+00:00Added an answer on June 4, 2026 at 9:13 pm

    You can use std::string as a parameter to construct a boost::asio::buffer. I typically use a char vector (std::vector<char>), though. I think it is easier to process it, but that may really depend on how the rest of your code is designed.

    You may also use a C++11 array. It behaves similar to a vector, but it statically allocates the space (i.e., the size of underlying buffer cannot change once the array is created). That might give you some performance benefits in some circumstances. If you cannot use C++11, Boost also includes a very similar class.

    boost::asio::buffer also accepts a normal char array (char buf[SIZE]), but it might be more convenient to use the previously mentioned options, if possible.

    For reference, here it is the documentation for boost::asio::buffer.

    Update: In order to avoid the conversion from char[] to string, you could use a vector both for receiving and processing the buffer (using a string for receiving the buffer may also work too, but I have never tried that).

    So, for receiving you could do:

    vector<char> msg(N);
    ...
    asio::read(socket, asio::buffer(msg), asio::transfer_at_least(N), ec);
    

    Then for processing the packet and splitting the header and the data, you could use iterators, avoiding the operations with O(n) complexity in your code (substr and erase). Of course find (or search in my example) cannot be avoided:

    string end_marker = "end";
    auto it = search(msg.begin(), msg.end(), end_marker.begin(), end_marker.end());
    
    process_header(msg.begin(), it + 2);
    process_data(it + 3, msg.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.