ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:17.30KB ,
资源ID:4019675      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-4019675.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Http Server C语言.docx)为本站会员(b****4)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

Http Server C语言.docx

1、Http Server C语言#include #include #include #include #include #include #include #include #include #include #include #include / 建立socket 开始侦听 接收连接 接收客户端数据 解析http协议 发送文件数据给客户端#define HTTP_PORT 2010#define MAX_CONNECTION 10#define DOCUMENT_ROOT www#define LOG_PATH log/access.logvoid parser(char *s,char r

2、es255,char host255);static char *strtoupper( char *s );static long filesize(const char *filename);static int file_exists(const char *filename);static void mime_content_type( const char *name, char *ret );static int WriteLog( const char *message );static int is_dir(const char *filename);static unsign

3、ed short g_is_log = 1;static int g_log_fd = 0;int main(void) int server_sock; int client_sock; struct sockaddr_in server_addr; struct sockaddr_in client_addr; struct sockaddr_in sin; struct stat file_stat; pid_t pid; char client_ip100; char buf20000; char buf_all2000; char buf12000; char p3255; char

4、 h3255; char tmp2000; char cwd1024; char filename2000; char filepath2000; int fd,size; int currentConn = 0; DIR * dir; struct dirent * ptr; chdir(./); if ( (pid = fork() 0 ) perror(fork); exit(1); else if ( pid != 0) exit(1); if(server_sock = socket(AF_INET,SOCK_STREAM,0) 0) perror(socket); exit(1);

5、 memset(&server_addr,0,sizeof(server_addr); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(atoi(HTTP_PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(server_sock,(struct sockaddr*)&server_addr,sizeof(server_addr) 0) perror(bind); exit(1); if(listen(server_sock,MAX_CONN

6、ECTION) 0) perror(listen); exit(1); printf(fasthttp successful created .n); while(1) unsigned int clientlen = sizeof(client_addr); if(client_sock = accept(server_sock,(struct sockaddr*)&client_addr,&clientlen) 0) perror(accept); exit(1); if(pid = fork() = 0) if(read(client_sock,buf,20000) 0) perror(

7、read data from client); exit(1); parser(buf,p,h); if(strcmp(strtoupper(p0),GET) != 0 & strcmp(strtoupper(p0),POST) != 0 & strcmp(strtoupper(p0),HEAD) != 0) memset(&buf,0,sizeof(buf); sprintf(buf, HTTP/1.1 501 Not ImplementedrnServer: %srnContent-Type: text/htmlrnContent-Length: 1489rnAccept-Ranges:

8、bytesrnConnection: closernrn, Apache); write(client_sock,buf,strlen(buf); memset(&buf,0,sizeof(buf); sprintf(buf,%s Method Not Implemented,501); write(client_sock,buf,strlen(buf); close(client_sock); exit(0); if(strcmp(p1,/) = 0) memset(&tmp,0,sizeof(tmp); sprintf(tmp,%s,index.html); strcat(p1,tmp);

9、 WriteLog(p1); getcwd(filepath, sizeof(filepath); strcat(filepath,/); strcat(filepath,DOCUMENT_ROOT); strcat(filepath,p1); if(!file_exists(filepath) memset(&buf,0,sizeof(buf); sprintf(buf, HTTP/1.1 404 Not FoundrnServer: %srnContent-Type: text/htmlrnContent-Length: 257271rnConnection: closernrn, Apa

10、che); write(client_sock,buf,strlen(buf); memset(&buf,0,sizeof(buf); sprintf(buf,404 Not Found404 Not FoundPowered by %s,fasthttp); write(client_sock,buf,strlen(buf); close(client_sock); memset(&buf,0,sizeof(buf); sprintf(buf,404 Not Foundt%sn,filepath); WriteLog(buf); exit(0); if(access(filepath,R_O

11、K) 0) memset(&buf,0,sizeof(buf); sprintf(buf, HTTP/1.1 403 ForbiddenrnServer: %srnContent-Type: text/htmlrnContent-Length: 25727rnConnection: closernrn, Apache); write(client_sock,buf,strlen(buf); close(client_sock); exit(0); /* 目录列表 */ if(is_dir(filepath) memset(&tmp,0,sizeof(tmp); sprintf(tmp,Inde

12、x of %sIndex of %s Parent Directory,filepath,filepath); strcat(buf,tmp); if(dir = opendir(filepath) != NULL) while(ptr = readdir(dir) != NULL) if(strcmp(ptr-d_name,.) = 0 | strcmp(ptr-d_name,.) = 0) continue; memset(&buf,0,sizeof(buf); sprintf(buf,%s/%s,filepath,ptr-d_name); if(is_dir(buf) memset(&b

13、uf,0,sizeof(buf); sprintf(buf, %s/,ptr-d_name,ptr-d_name); else memset(&buf,0,sizeof(buf); sprintf(buf, %s,ptr-d_name,ptr-d_name); strcat(tmp,buf); closedir(dir); memset(&buf,0,sizeof(buf); sprintf(buf,%s,); strcat(tmp,buf); memset(&buf,0,sizeof(buf); sprintf(buf, HTTP/1.1 200 OKrnServer: fasthttprn

14、Content-Type: text/html;charset=utf-8rnContent-Length: %drnConnection: closernrn, strlen(tmp); write(client_sock,buf,strlen(buf); write(client_sock,tmp,strlen(tmp); close(client_sock); memset(&tmp,0,sizeof(tmp); mime_content_type(filepath,tmp); memset(&buf,0,sizeof(buf); sprintf(buf, HTTP/1.1 200 OK

15、rnServer: %srnContent-Type: %srnContent-Length: 25727rnConnection: closernrn, Apache,tmp); write(client_sock,buf,strlen(buf); memset(&buf,0,sizeof(buf); fd = open(filepath,O_RDONLY); read(fd,buf,filesize(filepath); close(fd); write(client_sock,buf,filesize(filepath); close(client_sock); memset(&buf,

16、0,sizeof(buf); sprintf(buf,200 OKt%st%dn,filepath,filesize(filepath); WriteLog(buf); exit(0); else wait(NULL); close(client_sock); void parser(char *s,char res255,char host255) int i,j = 0; int n; char hosts255; for (i = 0;si != r;i+) /* obtain the first line in http protocol head */ ; si = 0; n=i+;

17、 for (i = 0,j = 0;i 3;i+,j+) /* divide the protocol head in blank */ strcpy(resj,strsep(&s, ); for(i=n;si != r;i+) strcat(hosts,si); for (i = 0,j = 0;i 3;i+,j+) /* divide the protocol head in blank */ strcpy(hostj,strsep(&hosts,:); /* * strtoupper - string to upper * */static char *strtoupper( char

18、*s ) int i, len = sizeof(s); for( i = 0; i = a & si = z ? si + A - a : si ); return(s);/* * filesize - get file size */static long filesize(const char *filename) struct stat buf; if (!stat(filename, &buf) return buf.st_size; return 0;/* * file_exists - check file is exist */static int file_exists(co

19、nst char *filename) struct stat buf; if (stat(filename, &buf) 0) if (errno = ENOENT) return 0; return 1;/* * Get MIME type header * */static void mime_content_type( const char *name, char *ret ) char *dot, *buf; dot = strrchr(name, .); /* Text */ if ( strcmp(dot, .txt) = 0 ) buf = text/plain; else i

20、f ( strcmp( dot, .css ) = 0 ) buf = text/css; else if ( strcmp( dot, .js ) = 0 ) buf = text/javascript; else if ( strcmp(dot, .xml) = 0 | strcmp(dot, .xsl) = 0 ) buf = text/xml; else if ( strcmp(dot, .xhtm) = 0 | strcmp(dot, .xhtml) = 0 | strcmp(dot, .xht) = 0 ) buf = application/xhtml+xml; else if

21、( strcmp(dot, .html) = 0 | strcmp(dot, .htm) = 0 | strcmp(dot, .shtml) = 0 | strcmp(dot, .hts) = 0 ) buf = text/html; /* Images */ else if ( strcmp( dot, .gif ) = 0 ) buf = image/gif; else if ( strcmp( dot, .png ) = 0 ) buf = image/png; else if ( strcmp( dot, .bmp ) = 0 ) buf = application/x-MS-bmp;

22、 else if ( strcmp( dot, .jpg ) = 0 | strcmp( dot, .jpeg ) = 0 | strcmp( dot, .jpe ) = 0 | strcmp( dot, .jpz ) = 0 ) buf = image/jpeg; /* Audio & Video */ else if ( strcmp( dot, .wav ) = 0 ) buf = audio/wav; else if ( strcmp( dot, .wma ) = 0 ) buf = audio/x-ms-wma; else if ( strcmp( dot, .wmv ) = 0 )

23、 buf = audio/x-ms-wmv; else if ( strcmp( dot, .au ) = 0 | strcmp( dot, .snd ) = 0 ) buf = audio/basic; else if ( strcmp( dot, .midi ) = 0 | strcmp( dot, .mid ) = 0 ) buf = audio/midi; else if ( strcmp( dot, .mp3 ) = 0 | strcmp( dot, .mp2 ) = 0 ) buf = audio/x-mpeg; else if ( strcmp( dot, .rm ) = 0 |

24、 strcmp( dot, .rmvb ) = 0 | strcmp( dot, .rmm ) = 0 ) buf = audio/x-pn-realaudio; else if ( strcmp( dot, .avi ) = 0 ) buf = video/x-msvideo; else if ( strcmp( dot, .3gp ) = 0 ) buf = video/3gpp; else if ( strcmp( dot, .mov ) = 0 ) buf = video/quicktime; else if ( strcmp( dot, .wmx ) = 0 ) buf = vide

25、o/x-ms-wmx; else if ( strcmp( dot, .asf ) = 0 | strcmp( dot, .asx ) = 0 ) buf = video/x-ms-asf; else if ( strcmp( dot, .mp4 ) = 0 | strcmp( dot, .mpg4 ) = 0 ) buf = video/mp4; else if ( strcmp( dot, .mpe ) = 0 | strcmp( dot, .mpeg ) = 0 | strcmp( dot, .mpg ) = 0 | strcmp( dot, .mpga ) = 0 ) buf = vi

26、deo/mpeg; /* Documents */ else if ( strcmp( dot, .pdf ) = 0 ) buf = application/pdf; else if ( strcmp( dot, .rtf ) = 0 ) buf = application/rtf; else if ( strcmp( dot, .doc ) = 0 | strcmp( dot, .dot ) = 0 ) buf = application/msword; else if ( strcmp( dot, .xls ) = 0 | strcmp( dot, .xla ) = 0 ) buf = application/msexcel; else if ( strcmp( dot, .hlp ) = 0 | strcmp( dot, .chm ) = 0 ) buf = application/mshelp; else if ( strcmp( dot, .swf ) = 0 | strcmp( dot, .swfl ) = 0 | str

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2