设为首页 加入收藏

TOP

Linux 用libevent实现的简单http服务器(一)
2019-07-09 20:11:19 】 浏览:48
Tags:Linux libevent 实现 简单 http 服务器

Linux 用libevent实现的简单http服务器

main.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include "libev.h"
#include <string.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>


int main(int argc, char** argv){

  // 端口
  int port = atoi(argv[1]);
  // 修改进程的工作目录, 方便后续操作
  int ret = chdir(argv[2]);
  if(ret == -1){
    perror("chdir error");
    exit(1);
  }
  
  struct event_base* base = NULL;
  base = event_base_new();
  if(base == NULL){
    perror("event_base_new");
    exit(1);
  }

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  struct evconnlistener* lis =
    evconnlistener_new_bind(base, listencb, base, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&addr, sizeof(addr));

  if(!lis){
    perror("evconnlistener_new_bind");
    exit(1);
  }

  event_base_dispatch(base);
  event_base_free(base);
}

libev.h

#ifndef __LIBEV__
#define __LIBEV__

#include <event2/bufferevent.h>
#include <event2/listener.h>


void listencb(struct evconnlistener* listener, evutil_socket_t fd,
          struct sockaddr* cli, int len, void* ptr);
void readcb  (struct bufferevent* bev,void* ctx);
void writecb (struct bufferevent* bev,void* ctx);
void eventcb (struct bufferevent* bev,short what,void* ctx);
#endif

libev.c

#include "libev.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "libev.h"
#include <event2/event.h>
#include <event2/bufferevent.h>
#include &
		    

lt;event2/buffer.h> #include <string.h> #include <sys/stat.h> #include <stdlib.h> #include <ctype.h> #include <fcntl.h> #include <unistd.h> #include <dirent.h> // 通过文件名获取文件的类型 const char *get_file_type(const char *name){ char* dot; // 自右向左查找‘.’字符, 如不存在返回NULL dot = strrchr(name, '.'); if (dot == NULL) return "text/plain; charset=utf-8"; if (strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0) return "text/html; charset=utf-8"; if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0) return "image/jpeg"; if (strcmp(dot, ".gif") == 0) return "image/gif"; if (strcmp(dot, ".png") == 0) return "image/png"; if (strcmp(dot, ".css") == 0) return "text/css"; if (strcmp(dot, ".au") == 0) return "audio/basic"; if (strcmp( dot, ".wav" ) == 0) return "audio/wav"; if (strcmp(dot, ".avi") == 0) return "video/x-msvideo"; if (strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt") == 0) return "video/quicktime"; if (strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe") == 0) return "video/mpeg"; if (strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl") == 0) return "model/vrml"; if (strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid") == 0) return "audio/midi"; if (strcmp(dot
编程开发网
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇leadcode的Hot100系列--62. 不同.. 下一篇Linux 用tcp实现的简单http服务器

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }