boost::asio
ネットワーク通信を簡易に行えるすごいライブラリ。
中の人がネットワークについてさっぱり理解していないので、これぐらいの認識です。
#include <iostream> #include <boost/asio.hpp> #include <boost/range/algorithm/for_each.hpp> #include <pstade/oven/stream_lines.hpp> int main(){ namespace asio = boost::asio; namespace oven = pstade::oven; // http://www.boost.org/LICENSE_1_0.txt を取得する asio::ip::tcp::iostream tcp_iso("www.boost.org", "http"); // 送信 tcp_iso << "GET /LICENSE_1_0.txt HTTP/1.0\r\n"; tcp_iso << "Host: www.boost.org\r\n"; tcp_iso << "\r\n"; tcp_iso << std::flush; // 受信 boost::for_each(oven::stream_lines(tcp_iso), [](const std::string& str){ std::cout << str << std::endl; }); return 0; }
出力:
HTTP/1.1 200 OK Date: Wed, 29 Sep 2010 02:30:07 GMT Server: Apache/2.0.52 (Red Hat) Last-Modified: Fri, 28 Mar 2008 17:26:33 GMT ETag: "110cc4a-53a-9a786440" Accept-Ranges: bytes Content-Length: 1338 Vary: Accept-Encoding Cache-Control: max-age=3000000, public, must-revalidate Expires: Fri, 31 Dec 2010 20:00:00 GMT Connection: close Content-Type: text/plain Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
今回は、受信したデータをそのまま出力してるけど、本来であれば受信したデータに付いてるヘッダー情報なんかも解析しないとダメみたい。
そこら辺は、魔導書の『Boost.AsioによるHTTP通信入門』に詳しく載っているので、本格的に通信をやりたいなら読んでみると面白い。
しかし、こうやって触ってると俄然、サーバ間通信とかやってみたくなる。
WindowsやLinuxなどの環境に左右されずに同じコーディングが出来るのがいいね。
例によってゲームの実機だとネットワーク周りの実装はどうなってるのかがすごく気になる。
まぁ最悪サーバ側だけでもこのライブラリで実装できるのかな?
[boost]
ver 1.44.0
[参照]
http://www.kmonos.net/alang/boost/classes/asio.html