ASIO例子HTTP客戶端,同步的例子sync_client.cpp

//
// sync_client.cpp
// ~~~~~~~~~~~~~~~
//HTTP客戶端,同步
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cout << "Usage: sync_client <server> <path>\n";
      std::cout << "Example:\n";
      std::cout << "  sync_client www.boost.org /LICENSE_1_0.txt\n";
      return 1;
    }

    boost::asio::io_service io_service;

    // Get a list of endpoints corresponding to the server name.
    //解析域名,獲取一個服務器列表。使用迭代器訪問
    tcp::resolver resolver(io_service);
    //http的意思使用80端口
    tcp::resolver::query query(argv[1], "http");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    // Try each endpoint until we successfully establish a connection.
    tcp::socket socket(io_service);
    //鏈接,這裏貌似並無遍歷,只是假設第一個端點ok
    boost::asio::connect(socket, endpoint_iterator);

    // Form the request. We specify the "Connection: close" header so that the
    // server will close the socket after transmitting the response. This will
    // allow us to treat all data up until the EOF as the content.
    //構建HTTP頭部?指定Connectioin:close以達到傳輸完響應後馬上關閉socket。這樣能夠容許
    //咱們把全部到EOF前數據當着內容
    //關於streambuf,參考:http://www.boost.org/doc/libs/1_56_0/doc/html/boost_asio/reference/streambuf.html
    //streambuf由std::streambuf派生而來,與streambuf的由一個或多個字符數組組成的輸入輸出序列相關。
    //貌似能夠對socket直接讀寫?
    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
    request_stream << "Host: " << argv[1] << "\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Connection: close\r\n\r\n";

    // Send the request.
    boost::asio::write(socket, request);

    // Read the response status line. The response streambuf will automatically
    // grow to accommodate the entire line. The growth may be limited by passing
    // a maximum size to the streambuf constructor.
    //讀取回應狀態行。響應streambuf將會自動增加直到完成整行。並且增加的長度限制在streambuf的max_size.
    //便可能一次read_until受streambuf的大小限制沒法讀完。
    boost::asio::streambuf response;
    boost::asio::read_until(socket, response, "\r\n");

    // Check that response is OK.
    //檢查響應報文,從響應報文讀取數據。
    std::istream response_stream(&response);
    std::string http_version;
    response_stream >> http_version;
    unsigned int status_code;
    response_stream >> status_code;
    std::string status_message;
    std::getline(response_stream, status_message);
    if (!response_stream || http_version.substr(0, 5) != "HTTP/")
    {
      std::cout << "Invalid response\n";
      return 1;
    }
    if (status_code != 200)
    {
      std::cout << "Response returned with status code " << status_code << "\n";
      return 1;
    }

    // Read the response headers, which are terminated by a blank line.
    //讀響應頭部,以空白行結束。
    boost::asio::read_until(socket, response, "\r\n\r\n");

    // Process the response headers.
    //輸出響應頭部
    std::string header;
    while (std::getline(response_stream, header) && header != "\r")
      std::cout << header << "\n";
    std::cout << "\n";

    // Write whatever content we already have to output.
    if (response.size() > 0)
      std::cout << &response;

    // Read until EOF, writing data to output as we go.
    //不停地讀socket直到讀取完畢。
    //transfer_at_least爲一個函數對象。即讀到指定的字節數即可以繼續下一次讀寫了。
    boost::system::error_code error;
    while (boost::asio::read(socket, response,
          boost::asio::transfer_at_least(1), error))
      std::cout << &response;
    if (error != boost::asio::error::eof)
      throw boost::system::system_error(error);
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << "\n";
  }

  return 0;
}
相關文章
相關標籤/搜索