I have a problem concerning boost asio libraries. I successfully tried to create a socket between a client and a server, this involves creation of resolvers in order to specify
Let's solve all 3 issues here:
Creating the iostream around the socket client side.
This is really simple:
boost::asio::ip::tcp::iostream socketStream;
socketStream.connect( hostname, std::to_string( port ) );
You have to check the state of the stream to see if it connected successfully.
Creating the iostream around the socket server side.
Assuming you have your acceptor object and it is bound and listening..
boost::asio::ip::tcp::iostream connectionSocketStream; // from the connection object
acceptor.accept( *connectionSocketStream.rdbuf() );
// or
acceptor.async_accept( *connectionSocketStream.rdbuf(), callback );
where callback is a function that takes an error code.
Streaming the objects
Now for the streaming itself and here your issue is that when you stream out the string "Message" the client side will need to know where this message begins and ends, and the regular iostream won't write anything to specify this. This is a flaw in iostream itself really.
The answer therefore is to use a boost archive, and you can use a text or binary archive as long as you use the same both ends. It even doesn't matter if one side is using 64-bit big-endian and the other side 32-bit little endian or any other mix.
Using binary archive you would send a message this way:
boost::archive::binary_oarchive oarch( socketStream, boost::archive::no_header );
oarch << "Message";
Remember to flush the stream (socketStream, not oarch) when you have completed sending all you wish to send at this point.
and receive a message
boost::archive::binary_iarchive iarch( socketStream, boost::archive::no_header );
iarch >> message;
You would potentially create one archive and use it throughout, especially for outbound. For inbound you may have issues if you get a streaming error as it will break your archive.
You can use a text archive instead of a binary one.
The boost archive will automatically put in header information so it knows when an object is complete and will only return to you once it has a complete object or something has broken.
Note: primitive types, eg std::string and even vector< int > etc. are automatically handled by an archive. Your own classes will need special overloads as to how to stream them. You should read boost::archive documentation.
Note: You can connect the archive object to the stream before the stream has been opened. The archive works around the streambuf object which does not change dependent on the stream opening successfully.
Creating without no_header would be an issue though as the archives immediately try to use the stream on construction (to read or write their header)