我的玻璃盒子

(转载)使用完成端口HTTP下载的代码

原文连接:http://www.cppblog.com/jinq0123/archive/2007/12/24/CompletionPortExamples.html


试运行asio的async_client例程时,发现CPU占用很高,
所以又写了一个相同功能但直接调用完成端口API的代码,
进行比较,发现同样占用CPU。
与flashget比较,下载速度差不多,但flashget不占CPU。
将直接API调用代码和利用asio的代码都列在下面。
进行测试时,要将其中的参数定义改改,如SERVER参数。
并且要找个大文件下载才有明显结果。
#include <iostream>
#include <winsock2.h>
// Modify these:
// "http://server.test.com/jinq/test.zip"
#define SERVER "server.test.com"
#define REQ_PATH "/jinq/test.zip"
const char * SVR_IP = "127.0.0.1";
int main(int argc, char* argv[])
{
    // Init.
    WSADATA wsd;
    WSAStartup(MAKEWORD(2, 2), &wsd);
    HANDLE hCp = CreateIoCompletionPort(
        INVALID_HANDLE_VALUE, NULL, 0, 0);
    SOCKET skt = WSASocket(AF_INET,
        SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
    assert(INVALID_SOCKET != skt);
    // connect skt and request
    SOCKADDR_IN addr;
    addr.sin_family = PF_INET;
    addr.sin_port = htons(80);
    addr.sin_addr.s_addr = inet_addr(SVR_IP);
    connect(skt, (SOCKADDR*)&addr, sizeof(addr));
    const char * REQ =
        "GET " REQ_PATH " HTTP/1.0\r\n"
        "Host: " SERVER "\r\n"
        "Accept: */*\r\n"
        "Connection: close\r\n\r\n";
    send(skt, REQ, strlen(REQ), 0);
    // Associate skt to completion port.
    const DWORD COMPLETION_KEY = 12345;
    CreateIoCompletionPort((HANDLE)skt,
        hCp, COMPLETION_KEY, 0);
    WSABUF wsaBuf;
    wsaBuf.len = 64 * 1024 - 1;
    wsaBuf.buf = new char[64 * 1024];
    DWORD dwReceived;
    DWORD dwFlags = 0;
    WSAOVERLAPPED overlapped;
    // Start recv.
    ZeroMemory(&overlapped, sizeof(overlapped));
    WSARecv(skt, &wsaBuf, 1,
            &dwReceived,
            &dwFlags,
            &overlapped,
            NULL);
    // Check the completion port in loop.
    while (true)
    {
        DWORD dwTransferred;
        LPOVERLAPPED lpOverlapped;
        DWORD dwKey;
        BOOL bRet = GetQueuedCompletionStatus(
            hCp, &dwTransferred, &dwKey, &lpOverlapped, 1000);
        if (!bRet) continue;
        assert(COMPLETION_KEY == dwKey);
        std::cout << "Transferred: " << dwTransferred << std::endl;
        assert(dwTransferred <= wsaBuf.len);
        wsaBuf.buf[50] = '\0';
        std::cout << "Content: " << wsaBuf.buf << std::endl;
        // next recv
        ZeroMemory(&overlapped, sizeof(overlapped));
        WSARecv(skt, &wsaBuf, 1,
                &dwReceived,
                &dwFlags,
                &overlapped,
                NULL);
    }
    return 0;
}
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
const std::string SERVER("MyServer");
const std::string PATH("/jinq/test.zip");
using boost::asio::ip::tcp;
class client
{
public:
    client(boost::asio::io_service& io_service,
           const std::string& server, const std::string& path)
            : socket_(io_service)
    {
        // Query server and try to connect.
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(server, "http");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;
        // Try each endpoint until we successfully establish a connection.
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
        {
            socket_.close();
            socket_.connect(*endpoint_iterator++, error);
        }
        if (error)
            throw boost::system::system_error(error);
        // Send the request.
        boost::asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << "GET " << path << " HTTP/1.0\r\n";
        request_stream << "Host: " << server << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";
        boost::asio::write(socket_, request);
        // start reading...
        boost::asio::async_read(socket_, response_,
            boost::asio::transfer_at_least(1),
            boost::bind(&client::handle_read_content, this,
                boost::asio::placeholders::error));
    }
private:
    void handle_read_content(const boost::system::error_code& err)
    {
        if (!err)
        {
            // Write all of the data that has been read so far.
            // std::cout << &response_ << "\n";
            std::cout << "Received: " << response_.size() << std::endl;
            response_.consume(response_.size());
            // Continue reading remaining data until EOF.
            boost::asio::async_read(socket_, response_,
                boost::asio::transfer_at_least(1),
                boost::bind(&client::handle_read_content, this,
                    boost::asio::placeholders::error));
        }
        else if (err != boost::asio::error::eof)
        {
            std::cout << "Error: " << err << "\n";
        }
    }
    tcp::socket socket_;
    boost::asio::streambuf response_;
};
int main(int argc, char* argv[])
{
    try
    {
        boost::asio::io_service io_service;
        client c(io_service, SERVER, PATH);
        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cout << "Exception: " << e.what() << "\n";
    }
    return 0;
}

posted on 2008-01-29 12:40 深蓝色系统 阅读(387) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


导航

<2009年8月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

统计

常用链接

留言簿(75)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜