Create an iostream using boost asio specifying ip and port

限于喜欢 提交于 2019-12-03 03:26:48

I've written a client/server system using Boost.Asio. The source is available on GitHub: Client.cpp and Server.cpp. Using Boost.Serialization together with Boost.Asio allows me to send arbitrary datastructures over the wire. I must say it is quite impressive!

The boost asio sample code you quoted:

tcp::iostream s(argv[1], "daytime");

uses "daytime" as a lookup into the services table (usually in /etc/services on a linux system), which would identify that the port for the daytime service is 13.

If you want to connect to a port that is not one of the well known services, you can do so with something like:

tcp::iostream s("localhost", "57002"); 

Note that the port number is supplied as a string, not as an unsigned short integer as one might be tempted to try.

Of course, "localhost" can be replaced with an IP address "127.0.0.1"

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!