tcp::resolver resolver(io_context); tcp::socket socket(io_context); tcp::resolver::results_type endpoints = resolver.resolve("localhost", "daytime"); boost::asio::connect(socket, endpoints); for (;;) { char message[1024]; boost::system::error_code error; size_t length = socket.read_some(boost::asio::buffer(message), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. net::MessageIn msg(message, length); std::string text; msg >> text; std::cout << "Received: " << text << std::endl; }
udp::endpoint sender_endpoint; char recv_buffer[1024]; size_t length = socket.receive_from( boost::asio::buffer(recv_buffer), sender_endpoint); net::MessageIn msg(recv_buffer, length); int value; msg >> value; std::cout << "Received: " << value << std::endl;This example receives a message from a UDP server and deserializes it into an integer using the MessageIn class. Package library: Boost.Asio