资源简介
linux 平台下实现一个简单httpserver 具体可以参考个人博客。

代码片段和文件信息
/*
* pub.c
*
* Created on: 2015-4-16
* Author: hsc
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include“pub.h“
#include“thread_work.h“
char LOGBUF[1024];
void save_log(char *buf)
{
FILE *fp = fopen(“log.txt““a+“);
fputs(buffp);
fclose(fp);
}
void setdaemon() //设置为守护进程
{
pid_t pid sid;
pid = fork();
if (pid < 0)
{
memset(LOGBUF0sizeof(LOGBUF));
sprintf(LOGBUF“fork failed %s\n“ strerror(errno));
save_log(LOGBUF);
exit (EXIT_FAILURE);
}
if (pid > 0)
{
exit (EXIT_SUCCESS);
}
if ((sid = setsid()) < 0)
{
printf(“setsid failed %s\n“ strerror(errno));
exit (EXIT_FAILURE);
}
/*if (chdir(“/“) < 0)
{
printf(“chdir failed %s\n“ strerror(errno));
exit(EXIT_FAILURE);
}*/
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
const char *get_filetype(const char *filename) //根据扩展名返回文件类型描述
{
////////////得到文件扩展名///////////////////
char sExt[32];
const char *p_start=filename;
memset(sExt 0 sizeof(sExt));
while(*p_start)
{
if (*p_start == ‘.‘)
{
p_start++;
strncpy(sExt p_start sizeof(sExt));
break;
}
p_start++;
}
////////根据扩展名返回相应描述///////////////////
if (strncmp(sExt “bmp“ 3) == 0)
return “image/bmp“;
if (strncmp(sExt “gif“ 3) == 0)
return “image/gif“;
if (strncmp(sExt “ico“ 3) == 0)
return “image/x-icon“;
if (strncmp(sExt “jpg“ 3) == 0)
return “image/jpeg“;
if (strncmp(sExt “avi“ 3) == 0)
return “video/avi“;
if (strncmp(sExt “css“ 3) == 0)
return “text/css“;
if (strncmp(sExt “dll“ 3) == 0)
return “application/x-msdownload“;
if (strncmp(sExt “exe“ 3) == 0)
return “application/x-msdownload“;
if (strncmp(sExt “dtd“ 3) == 0)
return “text/xml“;
if (strncmp(sExt “mp3“ 3) == 0)
return “audio/mp3“;
if (strncmp(sExt “mpg“ 3) == 0)
return “video/mpg“;
if (strncmp(sExt “png“ 3) == 0)
return “image/png“;
if (strncmp(sExt “ppt“ 3) == 0)
return “application/vnd.ms-powerpoint“;
if (strncmp(sExt “xls“ 3) == 0)
return “application/vnd.ms-excel“;
if (strncmp(sExt “doc“ 3) == 0)
return “application/msword“;
if (strncmp(sExt “mp4“ 3) == 0)
return “video/mpeg4“;
if (strncmp(sExt “ppt“ 3) == 0)
return “application/x-ppt“;
if (strncmp(sExt “wma“ 3) == 0)
return “audio/x-ms-wma“;
if (strncmp(sExt “wmv“ 3) == 0)
return “video/x-ms-wmv“;
return “text/html“;
}
int socket_create(int port)
{
int st = socket(AF_INET SOCK_STREAM 0);
int on =1;
if (st == -1)
{
memset(LOGBUF0sizeof(LOGBUF));
sprintf(LOGBUF“%s%d:socker error %s\n“ __FILE__ __LINE__ strerror(errno));
save_log(LOGBUF);
return 0;
}
if (setsockopt(st SOL_SOCKET SO_REUSEADDR &on sizeof(on)) == -1)
{
memset(LOGBUF0sizeof(LOGBUF));
sprintf(LOGBUF“setsockopt failed %s\n“ strerror(errno));
save_log(LOGB
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2550 2010-08-02 10:44 favicon.ico
文件 96974 2015-05-13 13:32 index.html
目录 0 2015-05-13 13:32 index_files\
文件 190931 2015-05-13 13:32 index_files\all_instant_search1_c1d4131e.js
文件 705 2015-05-13 13:32 index_files\baidu_jgylogo3.gif
文件 7877 2015-05-13 13:32 index_files\bd_logo1.png
文件 1224 2015-05-13 13:32 index_files\env_beb83b45.swf
文件 93680 2015-05-13 13:32 index_files\jquery-1.js
文件 1719 2015-05-13 13:32 index_files\JSocket_9a52fc3e.swf
文件 204 2015-05-10 11:11 Makefile
文件 5688 2015-05-13 20:37 pub.c
文件 368 2015-05-13 18:41 pub.h
文件 522 2015-05-13 18:36 server.c
文件 3032 2015-05-13 18:45 thread_work.c
文件 297 2015-05-10 14:06 thread_work.h
- 上一篇:NTFS数据恢复的c++类代码
- 下一篇:车牌识别源码
相关资源
- STM32F207通过http更新固件
- LINUX下命令行界面的C语言细胞游戏
- 尚观培训linux许巍老师关于c语言的课
- 《Linux程序设计》第四版pdf高清电子版
- QtWebApp
- linux 0.11内核代码
- linux ac108多麦方案驱动(ac108.c)
- 共享内存 读写
- 简易web服务器的设计与实现
- 《LINUX C编程从初学到精通》光盘源码
- Linux那些事儿之我是USB core
- 基于STM32F407的W5500 tcpserver(官网例程
- 高性能服务器代码(50_06th_server_thre
- C语言封装的HttpClient接口
- Linux c语言 学生成绩管理系统
- C++ sql2008 WebServer通讯.docx
- VC操作SQLSERVER数据库
- Linux开发工具手册
- c++开发http服务端+客户端
- 一个简单而强大的基于MFC的web server源
- Linux操作系统下C语言编程从零开始
- 基于Linux下C语言开发的员工管理系统
- 超级玛丽c++源码win32Linux平台
- UNIX/LINUX下C语言中文短信UCS2编码和解
- 嵌入式工程师必知必会 (完整高清中
- linux-2.6.24.rar
- Linux下C语言操作静态ARP表,包括增加
- c语言 linux 贪吃蛇.doc
- Linux多线程服务端编程:使用muduo C+
- libstdc++.so.6.0.23_linux7
评论
共有 条评论