<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title>meisw's blog</title>
		<link>http://meisw.wdlinux.cn//</link>
		<description>工作,学习,生活,这里将会有一些记录.     备用域名:http://meisw.wdlinux.cn</description>
		<copyright>Copyright (C) 2004 Security Angel Team [S4T] All Rights Reserved.</copyright>
		<generator>SaBlog-X Version 1.6 Build 20080806</generator>
		<lastBuildDate>Sat, 30 May 2026 01:05:46 +0000</lastBuildDate>
		<ttl>30</ttl>
		<item>
			<guid>http://meisw.wdlinux.cn//show-412-1.html</guid>
			<title>libevent 建立最简单的 webserver</title>
			<author>admin</author>
			<description><![CDATA[<p>libevent是一个事件触发的网络库，适用于windows、linux、bsd等多种平台，内部使用select、epoll、kqueue等系统调用管理事件机制。分布式缓存memcached据说也是libevent based，而且libevent在使用上可以做到跨平台，而且根据libevent官方网站上公布的数据统计，似乎也有着非凡的性能。</p>
<p>　　更多的信息可以去这里了解：<a href="http://baike.baidu.com/view/1590523.htm" target="_blank">http://baike.baidu.com/view/1590523.htm</a></p>
<p>　　先下载 libevent，官方地址是：<a href="http://monkey.org/~provos/libevent/" target="_blank">http://monkey.org/~provos/libevent/</a>，在下面有下载地址列表，我这里下载了<a href="http://monkey.org/~provos/libevent/doxygen-1.4.10/" target="_blank">1.4.10-stable</a>这个版本。</p>
<p>　　安装 libevnet：</p>
<pre class="code">#wget http://monkey.org/~provos/libevent-1.4.10-stable.tar.gz
#tar zxvf libevent-1.4.10-stable.tar.gz
#cd libevent-1.4.10-stable
#./configure
#make
#make install</pre>
<p>　　接下来看这个 c 语言示例程序 my_xhttpd.c：</p>
<p>&nbsp;</p>
<pre class="code">#include &lt;sys/types.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;sys/queue.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;sys/ioctl.h&gt;
#include &lt;ctype.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;
#include &lt;string.h&gt;
#include &lt;getopt.h&gt;
#include &lt;unistd.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;net/if.h&gt;
#include &lt;netdb.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;time.h&gt;
#include &lt;errno.h&gt;
#include &lt;assert.h&gt;
#include &lt;signal.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;err.h&gt;

// 引入 libevent 头文件
#include &lt;event.h&gt;
#include &lt;evhttp.h&gt;

// 请求处理模块
void http_handler(struct evhttp_request *req, void *arg)
{
  struct evbuffer *buf;
  buf = evbuffer_new();

  // 分析URL参数
  char *decode_uri = strdup((char*) evhttp_request_uri(req));
  struct evkeyvalq http_query;
  evhttp_parse_query(decode_uri, &amp;http_query);
  free(decode_uri);

  // 从http头中获取参数，如果是GET传输，这里就可以取得action的值
  const char *request_value = evhttp_find_header(&amp;http_query, &quot;data&quot;);

  // 返回给浏览器的信息
  evhttp_add_header(req-&gt;output_headers, &quot;Content-Type&quot;, 
&quot;text/html; charset=UTF-8&quot;);
  evhttp_add_header(req-&gt;output_headers, &quot;Server&quot;, &quot;my_httpd&quot;);
  //evhttp_add_header(req-&gt;output_headers, &quot;Connection&quot;, &quot;keep-alive&quot;);
  evhttp_add_header(req-&gt;output_headers, &quot;Connection&quot;, &quot;close&quot;);

  // 将要输出的值加入到输出缓存中
  if(request_value != NULL) {
    evbuffer_add_printf(buf, &quot;%s&quot;, request_value);
  } else {
    evbuffer_add_printf(buf, &quot;%s&quot;, &quot;no error.&quot;);
  }

  // 输出内容到浏览器
  evhttp_send_reply(req, HTTP_OK, &quot;OK&quot;, buf);

  // 内存释放
  evhttp_clear_headers(&amp;http_query);
  evbuffer_free(buf);
}

int main(int argc, char **argv)
{
  char *host_ip = &quot;0.0.0.0&quot;;
  int host_port = 8080;
  int timeout = 3;

  struct evhttp *httpd;

  event_init();

  // 绑定本机ip和端口，在访问时一定要把8080端口开放出来
  httpd = evhttp_start(host_ip, host_port);

  if (httpd == NULL) {
    fprintf(stderr, &quot;Error: Unable to listen on %s:%d\n\n&quot;, host_ip, host_port);		
    exit(1);		
  }

  // 设置请求超时时间
  evhttp_set_timeout(httpd, timeout);

  // 设置请求的处理函数
  evhttp_set_gencb(httpd, http_handler, NULL);

  event_dispatch();

  evhttp_free(httpd);

  return 0;
}</pre>
<p>　　编译源码：gcc -o my_httpd my_httpd.c -L/usr/local/lib/ -levent -I/usr/local/include/ -lz -lbz2 -lrt -lpthread -lm -lc -O2</p>
<p>　　编译成功后就会在当前目录生成一个 my_httpd 可执行文件。</p>
<p>　　放在后台启动：./my_httpd &gt;/dev/null 2&gt;&amp;1 &amp;</p>
<p>　　打开浏览器，输入访问地址，如图：</p>
<p>　　这只是个用来学习的简单示例，但 libevent 功能强大，有很多可以值的学习的地方。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-412-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-34-1.html">c</category>
			<pubDate>2011-05-26 19:20</pubDate>
		</item>
	</channel>
</rss>
