设为首页 加入收藏

TOP

基于EPOLL写的HTTP服务器(加入了线程池)(一)
2012-08-26 14:11:58 】 浏览:11814
Tags:基于 EPOLL HTTP 服务器 加入 线程
#include<fcntl.h>
#include<cstdio>
#include<unistd.h>
#include<cstdlib>
#include<sys/socket.h>
#include<sys/epoll.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<cstring>
#include<pthread.h>

const int EPOLL_SIZE=5000;
const int EVENT_ARR=5000;
const int PORT=8002;
const int BUF_SIZE=5000;
const int BACK_QUEUE=100;
const int THREAD_MAX=100;
static unsigned int s_thread_para[THREAD_MAX][8];   //线程参数
static pthread_t s_tid[THREAD_MAX];                 //线程ID
pthread_mutex_t s_mutex[THREAD_MAX]; //线程锁
int epFd;                            //epoll
struct epoll_event ev,evs[EVENT_ARR];

char *get_type(char *url,char *buf)
{

   const char *t=url+strlen(url);
   char type[64];
   for(;t>=url&&*t!='.';--t)  ;

   strcpy(type,t+1);
   if(strcmp(type,"html")==0||strcmp(type,"htm")==0)
           sprintf(buf,"text/%s",type);
   else if(strcmp(type,"gif")==0||
         strcmp(type,"jpg")==0||
         strcmp(type,"jpeg")==0||
         strcmp(type,"png")==0)
         sprintf(buf,"image/%s",type);
  else if(strcmp(type,"/")==0)
      sprintf(buf,"text/html");
  else if(strcmp(type,"css")==0)
        sprintf(buf,"text/css");
  else if(strcmp(type,"js")==0)
     sprintf(buf,"application/x-java script");
  else
      {
     
      sprintf( buf, "unknown" );
      }

      return buf;

}
 void* http_server(int thread_para[])
{
      int pool_index;           //thread pool ID
      int clientFd;             //client socket
      char buf[BUF_SIZE];
      pthread_detach(pthread_self());
      pool_index=thread_para[7];

 wait_unlock:
         pthread_mutex_lock(s_mutex+pool_index); //wait for thread unlock
         clientFd=thread_para[1];                //client socket ID
           &
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇 select, poll和epoll的区别 下一篇VC实现线程池

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目