Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337981
  • 博文数量: 45
  • 博客积分: 669
  • 博客等级: 上士
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-27 17:59
文章分类
文章存档

2015年(5)

2014年(6)

2013年(4)

2012年(30)

分类: LINUX

2012-08-03 12:22:01

本设计设计的web服务器,根据服务机上指定的网页为默认网页,存储于本地,从浏览器中输入WEB服务器的地址,服务器响应,并发送默认网页给浏览器,由于默认网页中包含图片的内容,当浏览器读到该照片内容时,再向服务器发送照片请求,服务器响应请求,把浏览器所需要的内容打开,发送给浏览器。

 

本设计牵涉到 http协议,需根据协议信息头,分辨浏览器发送的请求内容,再做答复。答复中,也需要遵守http协议,先发送协议头,以便浏览器辨认识别。本文中采用了sscanf截取浏览器发送的http协议头中的文件信息。


点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/socket.h> // 包含套接字函数库
  3. #include <stdio.h>
  4. #include <netinet/in.h> // 包含AF_INET相关结构
  5. #include <arpa/inet.h> // 包含AF_INET相关操作的函数
  6. #include <unistd.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9. #include<fcntl.h>
  10. #include<sys/stat.h>
  11. #include<dirent.h>


  12. #define PORT 80
  13. #define FILENAME "my.html"
  14. #define SIZE 100

  15. int main()
  16. {

  17.    
  18.    
  19.     int server_sockfd,client_sockfd;
  20.     int server_len,client_len;
  21.    
  22.     struct sockaddr_in server_sockaddr,client_sockaddr;
  23.    
  24.     server_sockfd = socket(AF_INET,SOCK_STREAM, 0); // 定义套接字类型
  25.    
  26.     server_sockaddr.sin_family=AF_INET;
  27.     server_sockaddr.sin_port=htons(PORT);
  28.     server_sockaddr.sin_addr.s_addr=INADDR_ANY;
  29.    
  30.     server_len=sizeof(server_sockaddr);
  31.    
  32.     //允许重复使用本地地址和套接字绑定
  33.     int j=1;
  34.     setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&j,sizeof(j));
  35.    
  36.     //绑定端口
  37.     if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1)
  38.     {
  39.         perror("bind:");
  40.         exit(1);
  41.     }
  42.     
  43.     //监听端口
  44.     if(listen(server_sockfd,5)==-1)
  45.     {
  46.         perror("listen:");
  47.         exit(1);
  48.     }
  49.     printf("Listening...\n");
  50.    
  51.    
  52.     unsigned char data_buf[2048];
  53.     int i;
  54.    
  55.     struct stat statbuf;
  56.    
  57.        
  58.     while(1)
  59.     {
  60.     if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_sockaddr,&client_len))==-1)
  61.         {
  62.              perror("accept error:");
  63.              exit(1);
  64.         }
  65.        
  66.     printf("%s linked\n",inet_ntoa(client_sockaddr.sin_addr));
  67.   
  68.    // printf("Send Msg...\n");
  69.    
  70.        
  71.     bzero(data_buf,sizeof(data_buf));
  72.     i=recv(client_sockfd,data_buf,sizeof(data_buf),0);
  73.    
  74.     printf("*********(%d)bytes recv******\n",i);
  75.     printf("data_buf=%s\n",data_buf);
  76.     printf("+++++++++++++++++++++++++++++\n");
  77.    
  78.     if(0==strncmp(data_buf,"GET / HTTP",10)) //发送网页文件
  79.     {
  80.         FILE *fp;
  81.         fp=fopen("my.html","r");
  82.         lstat("my.html",&statbuf);
  83.        
  84.         bzero(data_buf,sizeof(data_buf));
  85.         sprintf(data_buf,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %ld\r\n\r\n",statbuf.st_size);
  86.        
  87.         int file_length;
  88.         do
  89.         {
  90.            file_length=fread(data_buf,sizeof(char),2048,fp);
  91.            
  92.            i=send(client_sockfd,data_buf,strlen(data_buf),0);
  93.           // printf("send html bytes:i=%d\n",i);
  94.            bzero(data_buf,sizeof(data_buf));
  95.         }while(file_length>0);
  96.                       
  97.        
  98.         fclose(fp);
  99.         
  100.        
  101.     }
  102.    
  103.     else //根据浏览器的请求发送文件
  104.     {
  105.     char fileinfo[SIZE]; //获取请求的文件路径和文件名
  106.     bzero(fileinfo,100);
  107.     char buff[SIZE];
  108.     bzero(buff,sizeof(buff));
  109.    
  110.     sscanf(data_buf,"%*[^/]/%[^ ]",buff);
  111.     printf("file name buff=%s\n",buff);
  112.    
  113.     char a[2]={'/'};
  114.     //fileinfo[SIZE]={'/'};
  115.     strcat(fileinfo,a);
  116.     strcat(fileinfo,buff);
  117.       
  118.     printf("fileinfo=%s\n",fileinfo);
  119.    
  120.     //if(0==strncmp(data_buf,"GET /home/",10)) //发送图片
  121.     //{
  122.         printf("Send jpg...\n");
  123.         FILE *fp;
  124.         fp=fopen(fileinfo,"r");
  125.         if(fp==NULL)
  126.         {
  127.             perror("fopen failed:");
  128.         }
  129.         lstat(fileinfo,&statbuf); //获取文件属性
  130.    
  131.         bzero(data_buf,sizeof(data_buf));
  132.         sprintf(data_buf,"HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nContent-Length: %ld\r\n\r\n",statbuf.st_size);
  133.        
  134.         printf("statbuf.st_size=%ld\n",statbuf.st_size);
  135.        
  136.        
  137.         int file_block_length;
  138.               
  139.        do
  140.        {
  141.        file_block_length=fread(data_buf,1,2048,fp);
  142.        printf("file_block_length=%d\n",file_block_length);
  143.       
  144.       
  145.      // printf("data_buf=%s\n",data_buf);
  146.        i=send(client_sockfd,data_buf,2048,0);
  147.       
  148.        printf("send byte i=%d\n",i);
  149.        }while(file_block_length>0);
  150.       
  151.        printf("send over \n");
  152.        fclose(fp);
  153.    
  154.     }
  155.        
  156.     close(client_sockfd);
  157.     }
  158.    
  159. }

 

阅读(823) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~