資源簡介
使用C語言實現的io多路復用http服務器的一個簡單例子,可以顯示簡單的圖片文字等,內含makefile,所用到的圖片和html,編譯運行即可

代碼片段和文件信息
/*****************************************************************
?system:ubunt12.04
?enviroment:gcc
?date:20180724
?author:xiancan.wang
?*****************************************************************/
//for?exit()
#include??
//for?stdin??perro()
#include?
#include?
//socket
#include?
#include?
#include?
#include?
#include?
#define?MAXDATASIZE?1024
#define?SRC_PATH?“./data“
int?fileordirExist(char*?fpath)
{
????struct?stat?filestat;
printf(“fpath:%s\n“fpath);
????return?(??stat(fpath&filestat)?!=?-1);
}
//?is?the?filepath?a?directory
int?IsDIR(char*?fpath)
{
????struct?stat?filestat;
????return?(?stat(fpath&filestat)?!=?-1?&&?S_ISDIR(filestat.st_mode));
}
const?char*?getextname(const?char*?filepath)
{
????const?char*?p?=?NULL;
????if((?p?=?strrchr(filepath‘.‘))?!=?NULL)
?????????????return?p+1;
????return?NULL;???????????
}
//write?the?packet?header?to?the?client
int?writehead(FILE*?cfp?const?char*?extname)
{
????char*?content?=?“text/plain“;
????if(?strcmp(extname“html“)?==?0?||?strcmp(extname“htm“)?==?0)
????????content?=?“text/html“;
????else?if?(?strcmp(extname“css“)?==?0?)
????????content?=?“text/css“;
????else?if?(?strcmp(extname“gif“)?==?0?)
????????content?=?“image/gif“;
????else?if?(?strcmp(extname“jpeg“)?==?0?||?strcmp(extname“jpg“)?==?0)
????????content?=?“image/jpeg“;
????else?if?(?strcmp(extname“png“)?==?0)
????????content?=?“image/png“;
//必須以\r\n結尾
????fprintf(cfp“HTTP/1.1?200?OK?\r\n“);
????fprintf(cfp“Content-Type:?%s?\r\n“content);
//printf(“HTTP/1.1?200?OK?\r\n“);
//printf(“Content-Type:?%s?\r\n“content);
????return?0;
}
//send?the?data?of?the?file?which?the?client?want
void?sendobj(int?connectfdconst?char*?serverfilepath)
{
????FILE*?sfp?=NULL*cfp=NULL;
????int?c;
????sfp?=?fopen(serverfilepath“r“);
if(sfp?==?NULL)
{
printf(“erro?to?openfile?%s\n“serverfilepath);
return;
}
????cfp?=?fdopen(connectfd“w“);
????writehead(cfpgetextname(serverfilepath));
//發送完head開始發送body前必須先空一行
fprintf(cfp“\r\n“);
????while(?(c?=?getc(sfp))?!=?EOF)putc(ccfp);?
????fflush(cfp);
????return?;
}
//send?the?404?error?message?to?the?client
void?msg404(int?connectfd)
{
????const?char*?msg;
????msg??=?“HTTP/1.0?404?Not?Found?\r\n?Content-Type:?text/plain?404?not?found?by?Manio\r\n“;
????send(connectfdmsgstrlen(msg)0);
printf(“404\n“);
}
int?do_requst(int?connect_fd)
{
FILE?*net_fd?=?0;
char?requestline[MAXDATASIZE]={0};
char?cmd[128]={0};
char?filepath[512]={0};
//當前目錄作為根目錄
snprintf(filepathsizeof(filepath)SRC_PATH);
//重新以只讀方式打開
net_fd?=?fdopen(connect_fd“r“);?
fgets(requestlineMAXDATASIZEnet_fd);
printf(“Recv:\n***************\n%s***************\n“requestline);
//strlen,不計算‘\0‘
sscanf(requestline“%s?%s?“cmdfilepath+strlen(SRC_PATH));
//p
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????640??2018-07-24?16:47??demo0\Makefile
?????文件???????5701??2018-07-24?17:51??demo0\http.c
?????文件????????137??2018-07-24?16:32??demo0\data\index.html
?????文件??????57823??2018-07-20?16:46??demo0\data\p.png
????.......??????5780??2018-07-24?17:50??demo0\Release\http.o
?????文件??????12874??2018-07-24?17:50??demo0\Release\http_server
?????文件??????12874??2018-07-24?17:50??demo0\http_server
?????目錄??????????0??2018-07-24?15:13??demo0\data
?????目錄??????????0??2018-07-24?17:50??demo0\Release
?????目錄??????????0??2018-07-24?17:53??demo0\prj
?????目錄??????????0??2018-07-24?17:07??demo0
?????文件????????368??2018-07-24?17:53??demo0\prj\http_server.IMD
?????文件?????????39??2018-07-24?17:53??demo0\prj\http_server.SearchResults
?????文件???????4096??2018-07-24?17:53??demo0\prj\http_server.IAB
?????文件???????1136??2018-07-24?17:50??demo0\prj\http_server.PR
?????文件??????????8??2018-07-24?17:50??demo0\prj\http_server.PFI
?????文件???????4560??2018-07-24?17:50??demo0\prj\http_server.PRI
?????文件???????6360??2018-07-24?17:53??demo0\prj\http_server.PS
?????文件???????4096??2018-07-24?17:53??demo0\prj\http_server.IMB
?????文件??????18191??2018-07-24?17:53??demo0\prj\http_server.WK3
?????文件????????368??2018-07-24?17:53??demo0\prj\http_server.IAD
?????文件????????776??2018-07-24?17:50??demo0\prj\http_server.PO
-----------?---------??----------?-----??----
???????????????135827????????????????????22
- 上一篇:C語言圖書管理系統源代碼
- 下一篇:LSTM C++源代碼
評論
共有 條評論