金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
使用完成端口HTTP下载的代码

(转载请注明来源于金庆的专栏)

试运行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 2007-12-24 16:25 金庆 阅读(2318) 评论(4)  编辑 收藏 引用 所属分类: 1. C/C++

评论

# re: 使用完成端口HTTP下载的代码 2009-04-17 11:36 vvvb
ASIO内部也使用了IOCP模式的通信机制,因此应该是相似的结果  回复  更多评论
  

# re: 使用完成端口HTTP下载的代码 2011-08-19 15:34 金庆
只要有一个包到达,完成端口就执行一次从缓冲里读操作检测执行,而通常应用,一个数据段会由很多包组成,这样,内核线程需要调度很多次,而如果使用非阻塞 SOCKET读,通过定时检查机制,可以避免这种内部的频繁调度. 参见: 网络编程--走出完成端口的误区 ( http://blog.csdn.net/danscort2000/article/details/4703391 )  回复  更多评论
  

# re: 使用完成端口HTTP下载的代码 2013-04-17 00:30 cmi
通过运行博主的iocp写的下载代码,下载一个300m的文件,速度150~300k/s
cpu的占用率为0~1,没有出现楼主所说cpu占用率很高的情况。

博主还记得你的问题到底是什么原因引起的吗?

我在考虑是否要用iocp写,看了博主提供的这篇文章
http://blog.csdn.net/danscort2000/article/details/4703391 ,很是纠结。
我感觉这篇文章说的并不正确。  回复  更多评论
  

# re: 使用完成端口HTTP下载的代码 2013-12-16 16:39 金庆
@cmi
需要内网测试,速度接近网络带宽。  回复  更多评论
  


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