libevent的http服务器

本文介绍如何在ARM Linux环境下使用libevent库构建HTTP服务器。包括libevent的交叉编译配置、HTTP服务器代码实现及特定请求处理示例。通过示例代码了解POST请求处理流程,并掌握服务器的启动和停止方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

arm linux下用libevent搭建http服务器

1.下载安装libevent

libevent下载地址:https://libevent.org/
解压:libevent-2.1.12-stable.tar.gz
交叉编译: ./configure --host=arm-xmv2-linux-uclibcgnueabi CPPFLAGS="-I/opt/xm_toolchain/arm-xmv2-linux/usr/include" LDFLAGS="-L/opt/xm_toolchain/arm-xmv2-linux/usr/lib -lssl -lcrypto" CC=/opt/xm_toolchain/arm-xmv2-linux/usr/bin/arm-xmv2-linux-uclibcgnueabi-gcc CXX=/opt/xm_toolchain/arm-xmv2linux/usr/bin/arm-xmv2-linux-uclibcgnueabi-g++ --prefix=$PWD/__install
make
make install

使用libevent写http服务器代码

#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/queue.h>
#include <event2/thread.h>
/*******************************************************************************
 * Function Name : generic_handler
 * Create Date   : 2020-2-23
 * Author        : ZLF
 * Description   : 处理post请求
 * Param         :              
 * Return Code   : None
 *******************************************************************************/
static void http_handler_testpost_msg(struct evhttp_request *req,void *arg)
{
	char length[16]={0};
	char resBuf[200]={0};
	char post_data[1024];
	/*获取POST方法的数据*/
	memset(post_data,0,sizeof(post_data));
	size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
	strcpy(post_data,(char *) EVBUFFER_DATA(req->input_buffer));

	
	printf("%s get post_data, len =%ld\n",__FUNCTION__, post_size);
	printf("RCV:\n%s\n",post_data);

	if(!Deal_Http_Jsdata(post_data,(char *)arg)){
		Re_Jsdata(resBuf,1);
	}
	else{
		Re_Jsdata(resBuf,0);
	}
	evutil_snprintf(length, sizeof(length)-1, "%lu", strlen(resBuf));
	//evhttp_add_header(req->output_headers, "Server", MYHTTPD_SIGNATURE);
	evhttp_add_header(req->output_headers, "Content-Type", "application/json; charset=UTF-8");
	evhttp_add_header(req->output_headers, "Content-Length",length);
	evhttp_add_header(req->output_headers, "Connection", "close");
	
	//回响应
	struct evbuffer *buf;
	buf = evbuffer_new();
	evbuffer_add_printf(buf, "%s", resBuf);
	
	//将封装好的evbuffer 发送给客户端
	evhttp_send_reply(req, HTTP_OK, "OK", buf);
	evbuffer_free(buf);

}
/*******************************************************************************
 * Function Name : httpServer_Create
 * Create Date   : 2020-2-23
 * Author        : ZLF
 * Description   : 创建一个http服务器
 * Param         :              
 * Return Code   : None
 *******************************************************************************/
int httpServer_Create(void)
{
	//初始化一个http server
	struct event_base *eventbase;
	struct evhttp *ev_http;
	struct timeval ten_sec;
	
	ten_sec.tv_sec = 6000;
  	ten_sec.tv_usec = 0;

	char http_addr[50] = {0};
	
	getip(http_addr);
	printf("http_addr = %s\r\n",http_addr);
	
	evthread_use_pthreads();
	//初始化event_init
	eventbase = event_base_new();

	ev_http = evhttp_new(eventbase);

	evhttp_bind_socket(ev_http,"0.0.0.0",HTTP_LISTEN_PORT);
	//设置请求超时时间(s)
	evhttp_set_timeout(ev_http,5);
	//设置支持类型post
	evhttp_set_allowed_methods(ev_http,EVHTTP_REQ_POST);
	//特定回调函数http://10.8.0.42:8081/zhanglf
	evhttp_set_cb(ev_http,"/zhanglf",http_handler_testpost_msg,"zhanglf");
	//特定回调函数/detector/lens_adjust
	evhttp_set_cb(ev_http,"/detector/lens_adjust",http_handler_testpost_msg,"lens_adjust");
	//特定回调函数/detector/connection_config
	evhttp_set_cb(ev_http,"/detector/connection_config",http_handler_testpost_msg,"connection_config");
	//特定回调函数/detector/detect_config
	evhttp_set_cb(ev_http,"/detector/detect_config",http_handler_testpost_msg,"detect_config");
	//特定回调函数/detector/space_config /detector/push_config
	evhttp_set_cb(ev_http,"/detector/space_config",http_handler_testpost_msg,"space_config");
	//特定回调函数/detector/push_config
	evhttp_set_cb(ev_http,"/detector/push_config",http_handler_testpost_msg,"push_config");
	//特定回调函数/detector/light_config
	evhttp_set_cb(ev_http,"/detector/light_config",http_handler_testpost_msg,"light_config");
	//通用回调函数
	//evhttp_set_gencb(ev_http,generic_handler,NULL);

	printf("http server start OK! \n");

	//循环处理events
	event_base_loopexit(eventbase,&ten_sec);
	event_base_dispatch(eventbase);
	
	evhttp_free(ev_http);
	event_base_free(eventbase);
	printf("HTTP server quit\r\n");
	
	return 0;
}

可以使用curl做客户端,进行测试。

3.http服务器的打开和关闭
调用event_base_loopexit()或者event_base_loopbreak(),注意在创建时一定要添加evthread_use_pthreads()否则会退出不了。

/*******************************************************************************
 * Function Name : http_server_open
 * Create Date   : 2020-3-15
 * Author        : ZLF
 * Description   : open http服务器
 * Param         :          
 * Return Code   : None
 *******************************************************************************/
int server_open(void){

	//open http_server
	int ret;
	ret = httpServer_Create();
	if(ret){
		printf("create http server event failure\r\n");
		return 1;
	}
	else{
		server_state = 0;
	}
	return 0;
}
/*******************************************************************************
 * Function Name : http_server_close
 * Create Date   : 2020-3-15
 * Author        : ZLF
 * Description   : close http服务器
 * Param         :          
 * Return Code   : success=0,fail
 *******************************************************************************/
int server_close(void){

	//close http_server
	if(event_base_loopexit(ev_base,NULL) == 0){
	// if(event_base_loopbreak(ev_base) == 0){
		printf("exit http server event ok\r\n");
	}
	else{
		printf("exit http server event error\r\n");
		return -1;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值