New libevent: dns and http support

Last night Niels Provos announced the release of libevent 1.2. It comes with 2 new and useful features:

  • Non-blocking DNS resolution: previously you had to write your own, hunt down someone elses or uses threads to prevent DNS lookups from blocking your app. This came just at the right time for me as I was about to integrate someone elses non-blocking solution into my app.
  • Support for simple HTTP clients and servers: At first thought this may seem like unneeded bloat, and to many it might. I usually work on applications that lack a user interface and sometimes a simple http interface is very useful for debugging, or quickly adding some extra control to your application. This might even been robust enough to add a basic webservices like API to your application.

Non-blocking DNS example:










{
fprintf(stderr, );
exit(1);
}

{
 *addrs = addresses;
int i;
 (result != 0) {
printf();
exit(1);
}
 {
 (i = 0; i < count; i++) {
printf(, inet_ntoa(addrs[i]));
}
exit(0);
}
}

{
 (argc != 2)
usage();
event_init();
evdns_init();
evdns_resolve_ipv4(argv[1], 0, evdns_cb, NULL);
event_dispatch();
 (0);
}

http server example





{
 *buf;
buf = evbuffer_new();
 (buf == NULL)
err(1, );
evbuffer_add_printf(buf, );
evhttp_send_reply(req, HTTP_OK, , buf);
}

{
 *buf;
buf = evbuffer_new();
 (buf == NULL)
err(1, );
evbuffer_add_printf(buf, , evhttp_request_uri(req));
evhttp_send_reply(req, HTTP_OK, , buf);
}

{
 *httpd;
event_init();
httpd = evhttp_start(, 8080);

evhttp_set_cb(httpd, , root_handler, NULL);

evhttp_set_gencb(httpd, generic_handler, NULL);
event_dispatch();

evhttp_free(httpd);
 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值