设为首页 加入收藏

TOP

基于 libevent 开源框架实现的 web 服务器(二)
2019-05-23 14:37:26 】 浏览:254
Tags:基于 libevent 开源 框架 实现 web 服务器
;name>>%s\n
",file); 35 send_file(bufev,PATH_OF_404_); 36 } 37 38 } 39 40 void write_cb(struct bufferevent* bufev, void* arg) 41 { 42 struct sockaddr_in *cli = (struct sockaddr_in*)arg; 43 char buf[1024] = {0}; 44 printf("Sent respond to cli,Ip ->%s and Port ->%d\n", 45 inet_ntop(AF_INET,&(cli->sin_addr.s_addr), buf,sizeof(buf)), 46 ntohs(cli->sin_port) ); 47 } 48 49 void event_cb(struct bufferevent* bufev, short ev, void* arg) 50 { 51 printf("event_cb successful\n"); 52 if(ev & BEV_EVENT_EOF) 53 { 54 struct sockaddr_in *cli = (struct sockaddr_in*)arg; 55 char buf[1024] = {0}; 56 printf("Have client disconnect, Ip ->%s and Port ->%d\n", 57 inet_ntop(AF_INET,&(cli->sin_addr.s_addr), buf,sizeof(buf)), 58 ntohs(cli->sin_port) ); 59 60 } 61 if(ev & BEV_EVENT_ERROR ) 62 { 63 printf("******************************** Happy Error******************************\n"); 64 } 65 bufferevent_free(bufev); 66 } 67 68 void listener_cb(struct evconnlistener *listener, 69 evutil_socket_t fd, struct sockaddr* cli, 70 int cli_len, void* arg) 71 { 72 73 printf("<<<<<<<<<<<<<<<<<<<<,,,,,,,,, listener_cb successful\n"); 74 struct event_base* base = (struct event_base*)arg; 75 76 struct bufferevent* bufev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); 77 bufferevent_setcb(bufev, read_cb, write_cb, event_cb,cli); 78 bufferevent_enable(bufev,EV_READ); 79 80 } 81 82 83 struct evconnlistener* libev_start(struct event_base*base, const char* Ip,int Port) 84 { 85 86 struct sockaddr_in ser; 87 ser.sin_family = AF_INET; 88 ser.sin_port = htons(Port); 89 inet_pton(AF_INET,Ip,&(ser.sin_addr.s_addr)); 90 91 struct evconnlistener* ret = evconnlistener_new_bind(base, listener_cb, base, 92 LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, 128, 93 (struct sockaddr *)&ser, sizeof(ser)); 94 95 if(ret == NULL) 96 { 97 return NULL; 98 } 99 100 return ret; 101 } View Code

然后是 发送文件和目录的回调“

  文件的:

 1 #include "head.h"
 2 
 3 int send_file(struct bufferevent *bufev,char* file)
 4 {
 5 
 6 
 7     int ffd = open(file,O_RDONLY);
 8     if(-1 == ffd)
 9     {
10         printf("sourceFilePath : %s\n",file);
11         perror("open sourceFile error");
12 
13     }
14 
15     char file_read_buf[1024];
16     int read_len = 0;
17 
18     char * type = get_file_type(file);
19 
20 
21     send_html_head(bufev,200, "OK", type);
22 
23     while((read_len=read(ffd, file_read_buf,sizeof(file_read_buf))) > 0)
24     {
25         if(0 == read_len)
26         {
27             break;
28         }
29         bufferevent_write(bufev,file_read_buf,read_len);;
30         file_read_buf[strlen(file_read_buf)+1] = '\n';
31         printf("send message :%s\n",file_read_buf);
32         memset(file_read_buf,0,sizeof(file_read_buf));
33     }
34 
35     printf("close ...\n");
36     close(ffd);
37     return 0;
38 }
View Code

  目录的(这里拼接网页的时候要细心 非常细心的那种 ):

 1 // File Name: _send_dir.c
 2 // Author: jiujue
 3 // Created Time: 2019年04月05日 星期五 19时27分18秒
 4 
 5 #include "head.h"
 6 #define MAXFORGTML_ 4096
 7 
 8 
 9 int send_dir_asheml(struct bufferevent *bufev, char *dirname, void* ar
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Devc++编程过程中的一些报错总结 下一篇小饭店预约登记管理系统

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目