Linux——实现网站发布服务器
2017-11-17 21:06
260 查看
1、DNS工作原理
1、 递归式
2、 迭代式
网页发布服务器
1.查看系统的服务程序的状态:
service name status
启动服务:
service name start
关闭服务:
service name stop
2、Linux上html默认存放路径:
/var/www/html
http请求报头:
HTTP应答报文段:
网站发布服务器代码 httpd.c:
1、 递归式
2、 迭代式
网页发布服务器
1.查看系统的服务程序的状态:
service name status
启动服务:
service name start
关闭服务:
service name stop
2、Linux上html默认存放路径:
/var/www/html
http请求报头:
HTTP应答报文段:
网站发布服务器代码 httpd.c:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <fcntl.h> #include <sys/stat.h> void send_404(int c) { char sendbuff[1024] = {0}; strcpy(sendbuff, "HTTP/1.1 404 Not Found\r\n"); strcat(sendbuff, "Server: myhttpd/1.0\r\n"); strcat(sendbuff, "Content-Length: 14\r\n"); strcat(sendbuff, "Content-Type: text/html;charset=gbk\r\n"); strcat(sendbuff, "\r\n"); strcat(sendbuff, "Page Not Found"); send(c, sendbuff, strlen(sendbuff), 0); } void senderr(int c) { send_404(c); } void send_success(int c, int fd) { struct stat st; fstat(fd, &st); char sendbuff[1024] = {0}; strcpy(sendbuff, "HTTP/1.1 200 OK\r\n"); strcat(sendbuff, "Server: myhttpd/1.0\r\n"); strcat(sendbuff, "Content-Length: "); sprintf(sendbuff+strlen(sendbuff), "%d", st.st_size); strcat(sendbuff, "\r\n"); strcat(sendbuff, "Content-Type: text/html;charset=UTF-8\r\n"); strcat(sendbuff, "\r\n"); send(c, sendbuff, strlen(sendbuff), 0); //fa song wen jian nei rong while(1) { char buff[10] = {0}; int n = read(fd, buff, 9); if(n <= 0) { break; } send(c, buff, n, 0); } close(fd); } void main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); assert(sockfd != -1); struct sockaddr_in ser, cli; memset(&ser, 0, sizeof(ser)); ser.sin_family = AF_INET; ser.sin_port = htons(80); ser.sin_addr.s_addr = inet_addr("192.168.1.120"); int res = bind(sockfd, (struct sockaddr*)&ser, sizeof(ser)); assert(res != -1); listen(sockfd, 5); while(1) { int len = sizeof(cli); int c = accept(sockfd, (struct sockaddr*)&cli, &len); assert(c != -1); printf("one client link\n"); while(1) { char buff[1024] = {0}; int n = recv(c, buff, 1023, 0); if(n <= 0) { break; } //printf("%s\n", buff); char *p = strtok(buff, " "); if(p == NULL) { senderr(c); continue; } p = strtok(NULL, " "); if(p == NULL) { senderr(c); continue; } char path[128] = "/var/www/html"; strcat(path, p); int fd = open(path, O_RDONLY); if(fd == -1) { send_404(c); continue; } //cheng gong, fa song ying da bao wen duan he wen jian nei rong send_success(c, fd); } close(c); printf("one client unlink\n"); } }
相关文章推荐
- Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!
- win7电脑做服务器,搭建网站应用后发布到互联网的实现过程
- 购买、配置阿里云Linux服务器配置ftp发布网站全教程
- 一台linux服务器,通过apache实现,一个域名访问jsp的网站,一个域名访问php的网站
- 使用Rome实现网站RSS发布的简单步骤
- Linux下各类TCP网络服务器的实现源代码
- 网站发布时出错-"服务器应用程序不可用",解决方法
- linux中建立网站服务器详解
- linux中建立网站服务器详解 推荐
- 利用Squid反相代理缓存实现网站内容静态发布
- 基于44B0平台的uC-Linux Web服务器实现方法研究
- 通过NAT实现内网访问外网并发布内网服务器
- 服务器应用:实现Linux与宿主机之间的通信
- Linux 服务器集群系统实现方案详解
- Linux 服务器集群系统实现方案详解
- 使用Rome实现网站RSS发布的简单步骤
- 一台服务器建立多个网站实现
- 启动Linux 的时候实现oracle数据库与Tomcat web服务器与操作系统一同关闭与启动
- Linux下各类TCP网络服务器的实现源代码
- 利用vs2005发布asp.net网站到远程服务器上