基于epoll的web server

转载 2011年01月07日 23:15:00

我根据一个epoll的模型改了一个http server出来。只有129行,还可以精简不少,呵呵。
小测了一下,一秒钟处理了一万了请求。当然这里只是把现成的东西输出。没考虑到发送数据处理。和请求的解析。
注意了,epoll只基于linux 2.6内核的。其他平台不能用。





/*-------------------------------------------------------------------------------------------------
gcc -o httpd httpd.c -lpthread 
author: wyezl
2006.4.28
---------------------------------------------------------------------------------------------------*/

#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

#define PORT 8888
#define MAXFDS 5000
#define EVENTSIZE 100

#define BUFFER "HTTP/1.1 200 OK/r/nContent-Length: 5/r/nConnection: close/r/nContent-Type: text/html/r/n/r/nHello"

int epfd;
void *serv_epoll(void *p);
void setnonblocking(int fd)
{
    int opts;
    opts=fcntl(fd, F_GETFL);
    if (opts < 0)
    {
          fprintf(stderr, "fcntl failed/n");
          return;
    }
    opts = opts | O_NONBLOCK;
    if(fcntl(fd, F_SETFL, opts) < 0)
    {
          fprintf(stderr, "fcntl failed/n");
          return;
    }
    return;
}

int main(int argc, char *argv[])
{
    int fd, cfd,opt=1;
    struct epoll_event ev;
    struct sockaddr_in sin, cin;
    socklen_t sin_len = sizeof(struct sockaddr_in);
    pthread_t tid;
    pthread_attr_t attr;

    epfd = epoll_create(MAXFDS);
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    {
          fprintf(stderr, "socket failed/n");
          return -1;
    }
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void*)&opt, sizeof(opt));

    memset(&sin, 0, sizeof(struct sockaddr_in));
    sin.sin_family = AF_INET;
    sin.sin_port = htons((short)(PORT));
    sin.sin_addr.s_addr = INADDR_ANY;
    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) != 0)
    {
          fprintf(stderr, "bind failed/n");
          return -1;
    }
    if (listen(fd, 32) != 0)
    {
          fprintf(stderr, "listen failed/n");
          return -1;
    }

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    if (pthread_create(&tid, &attr, serv_epoll, NULL) != 0)
    {
          fprintf(stderr, "pthread_create failed/n");
          return -1;
    }

    while ((cfd = accept(fd, (struct sockaddr *)&cin, &sin_len)) > 0)
    {
          setnonblocking(cfd);
          ev.data.fd = cfd;
          ev.events = EPOLLIN | EPOLLET;
          epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
          //printf("connect from %s/n",inet_ntoa(cin.sin_addr));
          //printf("cfd=%d/n",cfd);
    }

    if (fd > 0)
          close(fd);
    return 0;
}

void *serv_epoll(void *p)
{
    int i, ret, cfd, nfds;;
    struct epoll_event ev,events[EVENTSIZE];
    char buffer[512];

    while (1)
    {
          nfds = epoll_wait(epfd, events, EVENTSIZE , -1);
          //printf("nfds ........... %d/n",nfds);
          for (i=0; i<nfds; i++)
          {
                if(events[i].events & EPOLLIN)
                {
                    cfd = events[i].data.fd;
                    ret = recv(cfd, buffer, sizeof(buffer),0);
                    //printf("read ret..........= %d/n",ret);

                    ev.data.fd = cfd;
                    ev.events = EPOLLOUT | EPOLLET;
                    epoll_ctl(epfd, EPOLL_CTL_MOD, cfd, &ev);
                }
                else if(events[i].events & EPOLLOUT)
                {
                    cfd = events[i].data.fd;
                    ret = send(cfd, BUFFER, strlen(BUFFER), 0);
                    //printf("send ret...........= %d/n", ret);

                    ev.data.fd = cfd;
                    epoll_ctl(epfd, EPOLL_CTL_DEL, cfd, &ev);
                    //shutdown(cfd, 1);
                    close(cfd);

                }
          }
    }
    return NULL;
}




下面是测试结果:

[yangjian2@localhost bin]$ ./ab -c 50 -n 10000 [url]http://202.108.xxx.xxx:8888/[/url]
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, [url]http://www.zeustech.net/[/url]
Copyright (c) 1998-2002 The Apache Software Foundation, [url]http://www.apache.org/[/url]

Benchmarking 202.108.xxx.xxx (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software:     
Server Hostname:     202.108.xxx.xxx
Server Port:         8888

Document Path:       /
Document Length:     5 bytes

Concurrency Level:     50
Time taken for tests:   0.921732 seconds
Complete requests:     10000
Failed requests:     0
Write errors:       0
Total transferred:     872088 bytes
HTML transferred:     50120 bytes
Requests per second:   10849.14 [#/sec] (mean)
Time per request:     4.609 [ms] (mean)
Time per request:     0.092 [ms] (mean, across all concurrent requests)
Transfer rate:       923.26 [Kbytes/sec] received

Connection Times (ms)
        min mean[+/-sd] median   max
Connect:     1   1   0.9     2     3
Processing:   1   2   0.5     2     4
Waiting:     0   1   0.3     1     3
Total:       4   4   0.2     4     5
WARNING: The median and mean for the initial connection time are not within a normal deviation
    These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
50%     4
66%     4
75%     4
80%     4
90%     4
95%     4
98%     5
99%     5
100%     5 (longest request)

 

http://blog.sina.com.cn/u/1181509184

相关文章推荐

一起来写web server 08 -- 多线程+非阻塞IO+epoll

到了多线程,一些东西就变得耐人寻味了. 这个版本是在前面单线程epoll的基础上引入了线程池,当然不是前面玩具一样的线程池,而是一个通用的组件,生产者消费者队列.生产者消费者队列生产者消费者问题是操作...

知识库 tomcat-A Simple Web Server static

1 应用之间的通信 To send byte streams, you must first call the Socket class’s getOutputStream method to ob...

大型网站后台架构的Web Server与缓存

1.1 Web server Web server 用来解析HTTP协议。当web服务器接收到一个HTTP请求时,会返回一个HTTP响应,例如送回一个HTML页面。为了处理一个请求,we...

【Python Web Server】Paste多线程服务器

Paste服务器 一、安装 【1】CentOS系统下,使用 sudo pip install paste 命令进行安装 【2】Mac系统下,采用相同的方式进行安装,会报错 ...

SharePoint debug - Unable to render the data. If the problem persists, contact your web server admin

使用SharePoint Designer创建了一个外部内容类型external content type,连接SQL Server数据库中的一个表。
  • shrenk
  • shrenk
  • 2014-12-10 20:58
  • 1357

CentOS 5.5 下安装Countly Web Server过程记录

CentOS 5.5 下安装Countly Web Server过程记录1. 系统更新与中文语言包安装2. 基本环境配置:2.1. NodeJS安装 依赖项安装yum -y install gcc g...

DM9051 SPI 接口以太网模块 + NuTinyM051 + uIP实现web server

最近在某個论坛裡,看到这篇STM32F103+DM9051_UIP_SPI to 以太网,透过該版主拿到了DM9051NP 以太网卡,刚好手上有Nuvoton NuTiny M051 和 M451,就...

fuzzing-03-Easy File Sharing Web Server7.2分析和利用

0x00前言 首先,为什么没有fuzzing-02。。。因为在写02的时候,发现还是重复第一篇的相关内容,写一半就停了,不过03是我第一次分析别人没分析过的软件,并且独立编写shellcode,所以...

FastCGI: A High-Performance Web Server Interface 中文翻译版本

原文地址:http://www.fastcgi.com/devkit/doc/fastcgi-whitepaper/fastcgi.htm          这篇文章是FastCgi的白皮书...
  • ColZer
  • ColZer
  • 2012-10-21 14:30
  • 1531

Web Server 和 HTTP协议

 Web Server 和 HTTP协议 May 17, 2015 一直在找实习,有点什么东西直接就在evernote里面记了,也没时间来更新到这里。找实习真是个蛋疼的事,一直找的是困难模式...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)