   boost::asio and async_write
  Hi. I'm having a problem figuring out how to send a message asynchronously with boost::asio. Specifically, I don't know how to keep track of the data that the socket sends and delete it / free it afterwards, or how to make sure the data doesn't get deallocated or go out of scope before the async_write finished. I figure I can free the packet in the async_write handler, but this still doesn't help me with scope or even creating the data properly. The examples use normal synchronous writes except for the buffer example which is beyond me :]Also, I've been using packed structs and was planning on just sending those over, but I realize the packet size might not be fixed. Can anyone point me to an example that is a little easier to understand or a little more documented than the boost examples?   
  The Chat example from the Boost::Asio examples page shows a common method of doing this.In this method, for each connection context (class chat_session in the example), you simply associate one buffer object for reads and a container (list, queue, deque) of buffer objects for writes.When you go to write data, you copy the user data into a buffer object and add it to the container (see chat_session::deliver in the example). When you read data, you simply reuse the buffer object for each packet (see chat_session::handle_read_header and chat_session::handle_read_body). If you need to keep more than one packet at a time, then you use a container of buffer objects for the reads as well like you do the writes (see chat_room::deliver).The buffer objects only ever go out of scope when the context for the connection is destroyed, so you never have to worry about the validity of that state. Since you are using a container of buffer objects for writes, you simply let the dtor handle the cleanup automatically after you remove the buffer object from the container (see chat_session::handle_write).The Chat example can be refactored to an even more generic (and simpler) example as well once you get the boost::asio concepts down. You could use a std::vector<char> as the base buffer type for your packets so you do not have to worry about memory allocation issues and can handle any sized packets. When you go to send data, you can simply std::copy the data in using a back_inserter if you wish or resize the vector manually and std::copy/memcpy directly. When you receive data, you must have called resize by hand first to ensure there is enough room to handle the incoming data stream. The logic that the Chat example uses is stream processing by exact byte count (all or nothing) rather than piece wise. There are boost::asio functions for handling the stream piece by piece as well.So, to summarize:* Study and learn the Chat example as your base first, since it's the best example to use for your issue.* Associate a buffer object for reading with each connection context. Reuse this object for all your reads or add it to a container if you need to track many packets at once.* Associate a container of buffer objects for writing with each connection context. Copy the user data into these objects for sending so the data always remains valid.* Everything else takes care of itself due to the asynchronous design. There might be gotchas here and there with specific logic you use, but that's part of the programming process.Good luck!   ;  I usually just made a struct that wrapped the individual socket:struct Client {  socket s;  char recv_buffer[4096];  char send_buffer[4096];};Then only ever have one pending send or receive. When creating connections, Client is instantiated, the rest is done as normal OO.The basic idea is that buffers belonging to Client live as long as it does.   