#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <errno.h>
#include "common.h"
#include "serversignal.h"
#include "server_epoll.h" static int open_socket(struct sockaddr_in* paddr);
static int accept_client(int sockfd, struct sockaddr_in* paddr); static int process_request(int connfd);
static volatile sig_atomic_t graceful=; #define HTTP_PORT 18080
#define BACK_LOG 50
#define MAX_FDS 100
#define SOCKLEN sizeof(struct sockaddr_in) #define err_log_exit()\
do{\
perror("server failed");\
fprintf(stderr, "file %s line %d\n", __FILE__, __LINE__);\
exit(EXIT_FAILURE);\
}while()\ #define err_msg_exit(msg)\
do{\
perror(msg);\
fprintf(stderr, "file %s line %d\n", __FILE__, __LINE__);\
exit(EXIT_FAILURE);\
}while()\ static int setnonblocking(int sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, )|O_NONBLOCK) == -)
{
return -;
} return ;
} int main(int argc, char *argv[])
{
signal_init();
int max_worker = ;
int child = ;
int epollfd = ;
struct sockaddr_in saddr;
int sockfd = ;
int nfds = ;
int index = ;
int fd = ;
int acceptfd = ;
struct epoll_event *events; memset(&saddr, , sizeof(struct sockaddr_in)); sockfd=open_socket(&saddr); if(sockfd == -)
err_log_exit(); while(!graceful&&!child){
if(max_worker>){
switch(fork()){
case -:
err_log_exit();
break;
case :
child =;
break;
default:
printf("child creat\n");
max_worker--;
break;
}
}else{
int status =;
if( - != wait(&status)){
//max_worker++;
fprintf(stderr, "child quit\n");
}
}
} if(!child){
fprintf(stderr, "before quit, kill all child\n");
kill(, SIGINT);
sleep();
return ;
} //child
epollfd = server_epoll_create(MAX_FDS+);
if(epollfd == -)
err_log_exit(); if(server_epoll_event_add(epollfd, sockfd) == -)
err_log_exit(); events = (struct epoll_event*)malloc(MAX_FDS*sizeof(struct epoll_event));
memset(events, , MAX_FDS*sizeof(struct epoll_event)); /* close stdin and stdout, as they are not needed */
/* move stdin to /dev/null */
if (- != (fd = open("/dev/null", O_RDONLY))) {
close(STDIN_FILENO);
dup2(fd, STDIN_FILENO);
close(fd);
} /* move stdout to /dev/null */
if (- != (fd = open("/dev/null", O_WRONLY))) {
close(STDOUT_FILENO);
dup2(fd, STDOUT_FILENO);
close(fd);
} while(child&&!graceful){
nfds = epoll_wait(epollfd, events, MAX_FDS, );
index = ; while(index < nfds){ if(events[index].data.fd == sockfd){
acceptfd = accept_client(sockfd, &saddr);
//waking herd
if(acceptfd == -){
perror("accept failed\n");
}else{
//accept ok
if(server_epoll_event_add(epollfd, acceptfd) == -)
err_log_exit();
}
}else if(events[index].data.fd == acceptfd){
// receive data from client
// if client close, need avoid TIME_WAIT status
if(process_request(acceptfd) == ){
fprintf(stderr, "client close, close connection and quit listen connect fd\n");
if(server_epoll_event_del(epollfd, acceptfd) == -)
err_log_exit();
close(acceptfd);
}
}else{ }
index++;
}; if(nfds == -){
if (errno == EINTR)
continue;
else{
err_log_exit();
}
} }; return ;
} void server_graceful_set(int g)
{
if(g>){
g=;
}else{
g=;
}
graceful=g;
} int server_graceful_get()
{
return graceful;
} static int open_socket(struct sockaddr_in* paddr)
{
int sockfd = ;
struct sockaddr_in sockaddress; bzero(&sockaddress, sizeof(sockaddress)); if((sockfd = socket(AF_INET, SOCK_STREAM, )) == -)
err_log_exit(); sockaddress.sin_family = AF_INET;
sockaddress.sin_port = htons(HTTP_PORT); setnonblocking(sockfd); inet_pton(AF_INET, "10.174.8.163", &(sockaddress.sin_addr)); if(bind(sockfd, (struct sockaddr*)(&sockaddress), sizeof(sockaddress)) == -)
err_log_exit(); if(listen(sockfd, BACK_LOG) == -)
err_log_exit(); *paddr = sockaddress;
return sockfd;
} static int accept_client(int sockfd, struct sockaddr_in* paddr)
{
socklen_t len = SOCKLEN;
int connfd = ; if(paddr != NULL)
{
connfd = accept(sockfd, (struct sockaddr*)(paddr), &len);
}else
{
connfd = -;
}
return connfd;
} static int process_request(int connfd)
{
char request[];
int len = ;
bzero(request, sizeof(request));
len = recv(connfd, request, sizeof(request), );
if(len >)
fprintf(stderr, "%s\n", request); return len;
}
#include <unistd.h>
#include <string.h>
#include "server_epoll.h" int server_epoll_event_add(int epollfd, int sockfd)
{
struct epoll_event ep;
memset(&ep, , sizeof(ep));
ep.events = ;
ep.events |= EPOLLIN;
ep.events |= EPOLLOUT;
ep.data.fd = sockfd; return epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ep);
} int server_epoll_event_del(int epollfd, int sockfd)
{
struct epoll_event ep;
memset(&ep, , sizeof(ep));
ep.events = ;
ep.events |= EPOLLIN;
ep.events |= EPOLLOUT;
ep.data.fd = sockfd; return epoll_ctl(epollfd, EPOLL_CTL_DEL, sockfd, &ep);
} int server_epoll_create(int size)
{
int fd = epoll_create(size);
return fd;
} int server_epoll_close(int fd)
{
return close(fd);
}

webserver<2>的更多相关文章

  1. WebServer+ADO+百万数据查询

    很简单的demo,查询速度快,易理解,废话不说  上demo 看完就明白了 源码地址:http://files.cnblogs.com/files/SpadeA/WebDemo.zip 这是关于Web ...

  2. webserver[实时查询当天的天气情况]

    1.webserver是什么? 日常生活中经常会使用到webserver,注册时,会收到验证码,购买东西时,会收到短信,假如,A公司网站和B公司合作,那么A公司注册对的用户可以直接推送给B网站,那怎么 ...

  3. Warning: file_put_contents(data.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/test.php on line 22

    最近在学习PHP 在保存文件的时候报Warning: file_put_contents(data.txt): failed to open stream: Permission denied in ...

  4. [C#] 图文解说调用WebServer实例

    本文旨在实现如何在.NET环境下调用WebServer,以天气接口为例进行说明. WebServer地址:http://www.webxml.com.cn/WebServices/WeatherWeb ...

  5. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  6. C# 如何调用WebServer函数

    WebServer(ASMX)服务程序网站的编写简单总结. WebServer中遇到的问题 编写WebServer程序VS运行调试时如果出现 System.Data.OracleClient.Orac ...

  7. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

  8. 【监控】WebServer入库与缓存更新代码优化小计

    问题描述: 通过WebServer将监控数据入库到Hbase,在入库之前需要将指标与ip的列表更新到缓存中,以便前台页面随时选择查看.前两天上了一些新用户导致负载增加,逐渐发现某些用户的监控场景出现丢 ...

  9. Linux WebServer WebRoot Path Identification

    目录 . HTTPD(Apache) . NGINX . TENGINE . JBOSS . TOMCAT . LIGHTTPD 1. HTTPD(Apache) 0x1: 启动参数 Usage: . ...

  10. iOS - Apache Tomcat WebServer 服务器配置

    前言 提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提前准备好的软件: apache-tomcat-6.0.45.tar.gz eclip ...

随机推荐

  1. c随机数的产生(续)

    经过反复调试得出: srand((unsigned) time(0))函数提供的应该是程序启动的时间或某个固定时间,在一个程序中只需提供一次即可.rand()函数会自动记录处理 经过反复调试得出: s ...

  2. [置顶] c#对于文件的操作

    在system.iO:命名空间中提供了两种遍历枚举文件夹的类,Directory 和DirectoryInfo, 他们都有一个方法GetDirectories():来便利枚举文件夹, /// < ...

  3. 本机Font字体

    void getFontList() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Str ...

  4. A题笔记(11)

    No.1508  代码:https://code.csdn.net/snippets/192058 考察点:①char型字符转换成对应的 ASKII 的编码 可以通过这样对 string 的每一个字符 ...

  5. 利用putty实现文件在linux上传和下载

    利用putty实现文件上传和下载:1.打开windows命令提示符窗口d:(putty在d盘下)cd putty(pscp.exe所在目录)2:上传(主要利用pscp程序)pscp d:/jdk-8u ...

  6. Swift小功能点积累

    label字符加删除线 let label = UILabel(frame: CGRectMake(, , , )) let attr = NSMutableAttributedString(&quo ...

  7. 记录创建企业Wiki的几个开源项目

    很多著名的企业Wiki都是开源产品,如Foswiki(基于Perl),TWiki(基于Perl)等,另外有Mindtouch(基于PHP, C#),Confluence(基于Java)等.维基道的Wi ...

  8. 《sed的流艺术之三》-linux命令五分钟系列之二十三

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  9. sass中常用mixin

    //设置字体大小 @mixin font($s:14px,$h:1.5,$f:microsoft yahei){ font:$s/#{$h} $f; } //参数(方向,大小,颜色),默认:向下,10 ...

  10. js判断是否全是相同的字符串

    isSameStr("aa2a") //不都是相同的字符 function isSameStr(str){ var tem=0; for(var i=0;i<str.leng ...