符合说明的最简单的事情:
[Live On Coliru](http://coliru.stacked-crooked.com/a/e49a6536879aa345)
#include <boost/asio.hpp>#include <iostream>int main() { boost::system::error_pre ec; using namespace boost::asio; // what we need io_service svc; ip::tcp::socket sock(svc); sock.connect({ {}, 8087 }); // http://localhost:8087 for testing // send request std::string request("GET /newGame?name=david HTTP/1.1rnrn"); sock.send(buffer(request)); // read response std::string response; do { char buf[1024]; size_t bytes_transferred = sock.receive(buffer(buf), {}, ec); if (!ec) response.append(buf, buf + bytes_transferred); } while (!ec); // print and exit std::cout << "Response received: '" << response << "'n";}这将收到完整的响应。您可以使用虚拟服务器对其进行测试:(
也可以 在Coliru上运行):
netcat -l localhost 8087 <<< '"status":"okay", "id":"game-23", "letter":2'
这将表明已收到请求,并且响应将由上面的客户代码写出。
请注意,有关更多建议,您可以查看示例http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples.html(尽管它们专注于异步通信,因为这是阿西欧图书馆)



