相信大家对Apache都有所听闻,Apache是目前使用最为广泛我Web服务器。大家可以从news.netcraft.com/这个网站得到证实。

这是腾讯的uptime.netcraft.com/up/graph?site=www.qq.com.Apache强大的功能和高效的性能并且开放源代码的这种模式对我很有吸引力,但无赖自己水平有限,无法从Apache庞大的source code里面理清头绪。于是,我就冒出了个自己动手写一个小型的简单的Web服务器的想法,希望对这方面也和我一样感兴趣的朋友有所帮助。

 

我的实验环境为:

OS:Red Hat Enterprise Linux 5

gcc:4.1.2

libc:2.5

editor:Vim

lang:C

阅读该源代码需要以下预备知识:

C语言基础

Linux编程基础

socket编程基础(Linux)

TCP/IP基本原理

HTTP基本原理

关键字(Key Words):

Linux C, Web HTTP Server, Linux Socket.

-----------------------------------------------------------------------------------

下面是Mutu的第一个版本(0.1 Alpha),实现了WEB 服务器的最基本功能

包括以下源文件:

webserver.c----程序入口

init_socket.h init_socket.c----完成一些WEB服务器的初始化工作

get_time.h get_time.c----获得服务器的时间

http_session.h http_session.c----处理一次HTTP会话

以下是各文件源码:

webserver.c:

 

/*

* file:webserver.c

*/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<strings.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include"get_time.h"

#include"init_socket.h"

#include"http_session.h"

intmain(intargc,char*argv[])

{

intlisten_fd;

intconnect_fd;

structsockaddr_inserver_addr;

structsockaddr_inclient_addr;

bzero(&server_addr,sizeof(structsockaddr_in));

bzero(&client_addr,sizeof(structsockaddr_in));

if(init_socket(&listen_fd,&server_addr)==-1)

{

perror("init_socket() error. in webserver.c");

exit(EXIT_FAILURE);

}

socklen_taddrlen=sizeof(structsockaddr_in);

pid_tpid;

while(1)

{

if((connect_fd=accept(listen_fd,(structsockaddr*)&client_addr,&addrlen))==-1)

{

perror("accept() error. in webserver.c");

continue;

}

if((pid=fork())>0)

{

close(connect_fd);

continue;

}

elseif(pid==0)

{

close(listen_fd);

printf("pid %d process http session from %s : %d\n",getpid(),inet_ntoa(client_addr.sin_addr),htons(client_addr.sin_port));

if(http_session(&connect_fd,&client_addr)==-1)

{

perror("http_session() error. in webserver.c");

shutdown(connect_fd,SHUT_RDWR);

printf("pid %d loss connection to %s\n",getpid(),inet_ntoa(client_addr.sin_addr));

exit(EXIT_FAILURE);/* exit from child process, stop this http session  */

}

printf("pid %d close connection to %s\n",getpid(),inet_ntoa(client_addr.sin_addr));

shutdown(connect_fd,SHUT_RDWR);

exit(EXIT_SUCCESS);

}

else

{

perror("fork() error. in webserver.c");

exit(EXIT_FAILURE);

}

}

shutdown(listen_fd,SHUT_RDWR);

return0;

}

init_socket.h

/*

* file:init_socket.h

*/

#ifndefINIT_SOCKET_H

#defineINIT_SOCKET_H

#include<netinet/in.h>

#defineBACKLOG    20/* length of listening queue on socket */

#definePORT    8080/* web server listening port */

/* initialize the socket on server, include below

socket();

bind();

listen();

*/

/* listen_fd : the web server listen file decriptor

server_addr: the web server ipv4 address

RETURNS: success on 0, error on -1

*/

intinit_socket(int*listen_fd,structsockaddr_in*server_addr);

#endif

init_socket.c

/*

* file:init_socket.c

*/

#include<stdio.h>

#include<strings.h>

#include<unistd.h>

#include<netinet/in.h>

#include<sys/types.h>

#include<sys/socket.h>

#include"init_socket.h"

intinit_socket(int*listen_fd,structsockaddr_in*server_addr)

{

if((*listen_fd=socket(AF_INET,SOCK_STREAM,0))==-1)

{

perror("socket() error. in init_socket.c");

return-1;

}

/* set reuse the port on server machine  */

intopt=SO_REUSEADDR;

if(setsockopt(*listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt))==-1)

{

perror("setsockopt() error.  in init_socket.c");

return-1;

}

server_addr->sin_family=AF_INET;

server_addr->sin_port=htons(PORT);

server_addr->sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(*listen_fd,(structsockaddr*)server_addr,sizeof(structsockaddr_in))==-1)

{

perror("bind() error.  in init_socket.c");

return-1;

}

if(listen(*listen_fd,BACKLOG)==-1)

{

perror("listen() error.  in init_socket.c");

return-1;

}

return0;

}

get_time.h

/*

* file: get_time.h

*/

#ifndefGET_TIME_H

#defineGET_TIME_H

#defineTIME_BUFFER_SIZE    40/* buffer size of time_buffer  */

char*get_time_str(char*time_buf);

#endif

get_time.c

/*

* file:get_time.c

*/

#include<time.h>

#include<stdio.h>

#include<string.h>

#include"get_time.h"

/* get the time on server,

return: the ascii string of time , NULL on error

argument: time_buf the buffer to store time_string

*/

char*get_time_str(char*time_buf)

{

time_tnow_sec;

structtm*time_now;

if(time(&now_sec)==-1)

{

perror("time() in get_time.c");

returnNULL;

}

if((time_now=gmtime(&now_sec))==NULL)

{

perror("localtime in get_time.c");

returnNULL;

}

char*str_ptr=NULL;

if((str_ptr=asctime(time_now))==NULL)

{

perror("asctime in get_time.c");

returnNULL;

}

strcat(time_buf,str_ptr);

returntime_buf;

}

http_session.c

/*

* file: http_session.h

*/

#ifndefHTTP_SESSION_H

#defineHTTP_SESSION_H

#include<netinet/in.h>

#defineRECV_BUFFER_SIZE    1024/* 1KB of receive buffer  */

#defineSEND_BUFFER_SIZE    1050000/* 1.xMB of send buffer  */

#defineURI_SIZE            128/* length of uri request from client browse */

#defineTIME_OUT_SEC        10/* select timeout of secend */

#defineTIME_OUT_USEC        0/* select timeout of usecend */

#defineFILE_OK                200

#defineFILE_FORBIDEN        403/* there are no access permission*/

#defineFILE_NOT_FOUND        404/* file not found on server */

#defineUNALLOW_METHOD        405/* un allow http request method*/

#defineFILE_TOO_LARGE        413/* file is too large */

#defineURI_TOO_LONG        414/*  */

#defineUNSUPPORT_MIME_TYPE    415

#defineUNSUPPORT_HTTP_VERSION    505

#defineFILE_MAX_SIZE        1048576/* 1MB the max siee of file read from hard disk */

#defineALLOW"Allow:GET"/* the server allow GET request method*/

#defineSERVER"Server:Mutu(0.1 Alpha)/Linux"

/* if the connect protocol is http then this function deal with it  */

inthttp_session(int*connect_fd,structsockaddr_in*client_addr);

/* if http protocol return 1, else return 0 */

intis_http_protocol(char*msg_from_client);

/* get the request header's uri */

char*get_uri(char*req_header,char*uri_buf);

/* get the uri status,access return 0, not exist return 1, permission deny return 2, error return -1 */

intget_uri_status(char*uri);

/* get the mime type of the file request in uri from client's browse */

char*get_mime_type(char*uri);

/* read the file which requested by client in uri ,and store in entity_buf.

success return bytes readed,error return -1

*/

intget_file_disk(char*uri,unsignedchar*entity_buf);

/* set http replay header's status:

200:ok

404:file not found

*/

intset_rep_status();

intset_error_information(unsignedchar*send_buf,interrorno);

intreply_normal_information(unsignedchar*send_buf,unsignedchar*file_buf,intfile_size,char*mime_type);

#endif

如何访问该服务器呢?

首先你要知道运行服务器主机的IP,在服务器主机上输入如下命令(需要超级用户权限):

ifconfig

 

如果你的是以太网(ethernet),那么会看到这样一行

inet addr:xxx.xxx.xxx broadcast:xxx.xxx.xxx.xxx mask:255.xxx.xxx.xxx

xxx代表数字(000-255),第一个inet addr后面的数字便是你的网卡地址。

如果你是在本机进行测试,那IP地址可以直接用127.0.0.1(回环地址,localhost)

取得服务器的IP后,用你喜欢的一款浏览器便可以访问WEB SERVER的内容了。

方法为:在浏览器的地址栏内输入:

http://xxx.xxx.xxx.xxx:8080

回车,即可(xxx.xxx.xxx.xxx无刚取得的服务器IP地址,8080为预设的端口)。

点击了解更多资料,更有免费开源项目和课程等你观看哦!

C/C++编程日记:用C语言实现的简单Web服务器(Linux),全代码分享!的更多相关文章

  1. Socket网络编程--简单Web服务器(1)

    这一次的Socket系列准备讲Web服务器.就是编写一个简单的Web服务器,具体怎么做呢?我也不是很清楚流程,所以我找来了一个开源的小的Web服务器--tinyhttpd.这个服务器才500多行的代码 ...

  2. Socket网络编程--简单Web服务器(6)

    本来是想实现ssl连接的,但是弄了好久都不成功,就索性不做了,等以后有能力再做了.所以这一小节就是本次的最后一节了.就简单的说几个注意点. 1.加个配置文件 使用单例模式,使用一个类,该类保存一些信息 ...

  3. 伯克利SocketAPI(一) socket的C语言接口/最简单的服务器和对应的客户端C语言实现

    1. 头文件 2. API函数 3. 最简单的服务器和对应的客户端C语言实现 3.1 server #include <sys/types.h> #include <sys/sock ...

  4. Socket网络编程--简单Web服务器(5)

    这一小节我们将实现服务器对get和post的请求进行对cgi程序的调用.对于web服务器以前的章节已经实现了对get和post请求的调用接口,接下来给出对应接口的实现. int WebServer:: ...

  5. C/C++编程笔记:C语言开发球球大作战(源码分享),你想试试吗?

    游戏背景 <球球大作战>是Superpop一款自主研du发的免费手机网络游戏. 以玩家间的实时互动PK产生游戏乐趣为设计宗旨,通过简单的规则将玩家操作直接转化为游戏策略,体验智谋碰撞的战斗 ...

  6. Python网络编程(3)——SocketServer模块与简单并发服务器

    主要类型 该模块有四个比较主要的类,其中常用的是 TCPServer 和 UDPServer. 1. TCPServer 2. UDPServer 3. UnixStreamServer,类似于TCP ...

  7. Socket网络编程--简单Web服务器(2)

    上一小节通过阅读开源的Web服务器--tinyhttpd.大概知道了一次交互的请求信息和应答信息的具体过程.接下来我就自己简单的实现一个Web服务器. 下面这个程序只是实现一个简单的框架出来.这次先实 ...

  8. Socket网络编程--简单Web服务器(3)

    上一小节已经实现了浏览器发送请求,然后服务器给出应答信息,然后浏览器显示出服务器发送过来的网页.一切看起来都是那么的美好.这一小节就准备实现可以根据地址栏url的不同来返回指定的网页.目前还不考虑带参 ...

  9. Socket网络编程--简单Web服务器(4)

    上一小节已经实现了对图片的传输,接下来就是判断文件是否为js,css,png等格式.我们增加一个函数用于判断格式 int WebServer::get_filetype(char *type,char ...

随机推荐

  1. ByteCTF2019

    VIP 第一阶段: 先检查一下程序开的保护: 程序只开了canary和nx保护.接下来用IDA分析反编译出的伪代码 如上图,载edit函数中我们可以控制size的大小,并且程序没有做任何检查,我们再跟 ...

  2. Java中CAS 基本实现原理

    一.前言 了解CAS,首先要清楚JUC,那么什么是JUC呢?JUC就是java.util.concurrent包的简称.它有核心就是CAS与AQS.CAS是java.util.concurrent.a ...

  3. Web开发初探(系统理解Web知识点)

    一.Web开发介绍 我们看到的网页通过代码来实现的 ,这些代码由浏览器解释并渲染成你看到的丰富多彩的页面效果. 这个浏览器就相当于Python的解释器,专门负责解释和执行(渲染)网页代码. 写网页的代 ...

  4. python之类方法和静态方法

    在类中定义的函数称为方法,主要有三种:实例方法.类方法.静态方法. class MyTest(): # 普通实例函数 def func1(self, arg1, arg2): pass # 类函数 @ ...

  5. oracle之三闪回flashback

    闪回 flashback 5.1 flashback 的功能:1)利用undo data回溯或撤销提交的数据,2)flashback log 使database 可以恢复到过去某个时间点,可以作为不完 ...

  6. pycharm安装模块方法

    一. 打开pycharm 二. 点开file 三. 点击Settings,点击Project Interpreter,选择右上角+ 四. 进入后,在搜索框搜索需要安装的模块,选中安装 击Project ...

  7. CentOS6.10下安装MongoDB和Redis

    安装mongodb 首先考虑离线安装,但是安装过程中在启动服务的时候出现了问题,centOS出于稳定原因考虑,系统自带的glibc版本过低, 而编译需要使用较高版本,这个问题我查询了一下,需要升级gl ...

  8. 面试官问:Mybatis中的TypeHandler你用过吗?

    持续原创输出,点击上方蓝字关注我吧 目录 前言 环境配置 什么是TypeHandler? 如何自定义? 如何将其添加到Mybatis中? XML文件中如何指定TypeHandler? 源码中如何执行T ...

  9. .NET Core开源导入导出库 Magicodes.IE 2.3发布

    在2.3这一版本的更新中,我们迎来了众多的使用者.贡献者,在这个里程碑中我们也添加并修复了一些功能.对于新特点的功能我将在下面进行详细的描述,当然也欢迎更多的人可以加入进来,再或者也很期待大家来提is ...

  10. 注解&反射

    什么是注解 Annotation是从JDK5.0开始引入的新技术 Annotation的作用: 不是程序本身,可以对程序作出解释.(这一点和注释(comment)没什么区别) > 可以被其他程序 ...