跨平台,C / C ++ HTTP异步功能库 [英] Cross platform , C/C++ HTTP library with asynchronous capability

查看:212
本文介绍了跨平台,C / C ++ HTTP异步功能库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个C / C ++库,将在Windows和Linux的工作,这将让我异步地查询多个Web服务器(1000每分钟)的页头和下载网页在大致相同的方式WINHTTP库做的Windows环境。

I'm looking for a C/C++ library that will work on Windows and Linux which will allow me to asychronously query multiple webservers (1000's per minute) for page headers and download web pages in much the same way WinHttp library does in a windows environment.

到目前为止,我已经遇到的libcurl这似乎做我想做的,但异步方面看起来嫌疑。

So far I've come across libCurl which seems to do what I want but the asychronous aspect looks suspect.

如何容易,你认为它会绕过使用图书馆的想法,并写东西从基于套接字刮简单,可以实现这一目标?

How easy do you think it would be to bypass the idea of using a library and write something simple from scratch based on sockets that could achieve this?

任何意见,建议或建议将是非常欢迎的。

Any comments, advice or suggestions would be very welcomed.

附录: - 任何机构对与libcurl的这样的意见,我说的是异步方面可能看起来嫌疑人,但没有人有它的任何经验

Addendum:- Any body have comments about doing this with libCurl, I said the asychronous aspect may look suspect but does anyone have any experience of of it?

推荐答案

尝试 libevent的 HTTP例程。您创建一个HTTP连接,并且在一个响应到达提供被调用的回调(或超时事件触发)。

Try libevent HTTP routines. You create an HTTP connection and provide a callback which is invoked when a response arrives (or timeout event fires).

更新:我建了一个分布式的HTTP连接节流代理,并且使用了日
同样的守护进程中的E客户和服务器部分,全部在单个线程。它
伟大的工作。

Updated: I built a distributed HTTP connection-throttling proxy and used both th e client and server portions within the same daemon, all on a single thread. It worked great.

如果你正在写一个HTTP客户端,libevent的应该是一个不错的选择。唯一的
限制我跑进与服务器端是缺乏配置选项 -
如果你要开始添加更多高级功能的API是一个有点稀疏;这是我意料之中的,因为它从来没有打算取代通用的Web服务器,如Apache,Nginx的。比如我修改它来添加自定义的子程序来限制的总体规模
入站HTTP请求(10MB读后例如紧密连接)。在code是非常精心编写的补丁是很容易实现的。

If you're writing an HTTP client, libevent should be a good fit. The only limitation I ran into with the server side was lack of configuration options -- the API is a bit sparse if you want to start adding more advanced features; which I expected since it was never intended to replace general-purpose web servers like Apache, Nginx. For example I patched it to add a custom subroutine to limit the overall size of an inbound HTTP request (e.g. close connection after 10MB read). The code is very well-written and the patch was easy to implement.

我用的是1.3.x的分支; 2.x的分支有一些严重的性能
改进
的比旧的版本。

I was using the 1.3.x branch; the 2.x branch has some serious performance improvements over the older releases.

code例如:找到了几分钟,写了一个简单的例子。这应该让你的libevent的编程风格结识:

Code example: Found a few minutes and wrote a quick example. This should get you acquainted with the libevent programming style:

#include <stdio.h>
#include <event.h>
#include <evhttp.h>

void
_reqhandler(struct evhttp_request *req, void *state)
{
    printf("in _reqhandler. state == %s\n", (char *) state);
    if (req == NULL) {
        printf("timed out!\n");
    } else if (req->response_code == 0) {
        printf("connection refused!\n");
    } else if (req->response_code != 200) {
        printf("error: %u %s\n", req->response_code, req->response_code_line);
    } else {
        printf("success: %u %s\n", req->response_code, req->response_code_line);
    }
    event_loopexit(NULL);
}

int
main(int argc, char *argv[])
{
    const char *state = "misc. state you can pass as argument to your handler";
    const char *addr = "127.0.0.1";
    unsigned int port = 80;
    struct evhttp_connection *conn;
    struct evhttp_request *req;

    printf("initializing libevent subsystem..\n");
    event_init();

    conn = evhttp_connection_new(addr, port);
    evhttp_connection_set_timeout(conn, 5);
    req = evhttp_request_new(_reqhandler, (void *)state);
    evhttp_add_header(req->output_headers, "Host", addr);
    evhttp_add_header(req->output_headers, "Content-Length", "0");
    evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/");

    printf("starting event loop..\n");
    event_dispatch();

    return 0;
}

编译并运行:

% gcc -o foo foo.c -levent
% ./foo    
initializing libevent subsystem..
starting event loop..
in _reqhandler. state == misc. state you can pass as argument to your handler
success: 200 OK

这篇关于跨平台,C / C ++ HTTP异步功能库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆