首页 新闻 搜索 专区 学院

用C写的http服务器怎么返回图片给浏览器呢

0
悬赏园豆:10 [已关闭问题] 关闭于 2013-04-18 13:09
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/socket.h>
  4 #include <netinet/in.h>
  5 #include <string.h>
  6 #include <time.h>
  7 
  8 #define SERVER_NAME "CFWS"
  9 #define PROTOCOL "HTTP/1.0"
 10 #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
 11 #define BUFFSIZE 1024
 12 
 13 /*
 14   返回文件类型
 15   @param filename 文件名称
 16 */
 17 static char* get_mime_type(char *filename)
 18 {
 19   char *postfix;
 20   postfix = strrchr(filename, '.');
 21   if (!strcmp(postfix, ".html") || !strcmp(postfix, ".htm")){
 22     return "text/html; charset=UTF-8";
 23   }else if (!strcmp(postfix, ".jpg") || !strcmp(postfix, ".jpeg")){
 24     return "image/jpeg";
 25   }else if (!strcmp(postfix, ".css")){
 26     return "text/css";
 27   }else if (!strcmp(postfix, ".gif")){
 28     return "image/gif";
 29   }else if (!strcmp(postfix, ".png")){
 30     return "image/png";
 31   }else if (!strcmp(postfix, ".wav")){
 32     return "audio/wav";
 33   }else if (!strcmp(postfix, ".au")){
 34     return "audio/basic";
 35   }else if (!strcmp(postfix, ".avi")){
 36     return "video/x-msvideo";
 37   }else if (!strcmp(postfix, ".mp3")){
 38     return "audio/mpeg";
 39   }else{
 40     return "text/plain; charset=UTF-8";
 41   }
 42 }
 43 /*
 44   返回http头信息
 45 */
 46 static char* send_header(int status, char *title, char *mime_type, off_t length, char *buffter)
 47 {
 48 
 49   // char buffter[255];
 50   char tmp[100];
 51   char timebuff[55];
 52   time_t now;
 53   now = time((time_t*)0);
 54   memset(timebuff, 0, sizeof(timebuff));
 55   memset(buffter, 0, sizeof(buffter));
 56   sprintf(tmp, "%s %d %s\r\n", PROTOCOL, status, title);
 57   strcat(buffter, tmp);
 58   strftime(timebuff, sizeof(timebuff), RFC1123FMT, gmtime(&now));
 59   (void) sprintf(tmp, "Date: %s\r\n", timebuff);
 60   strcat(buffter, tmp);
 61   sprintf(tmp, "Server: %s\r\n", SERVER_NAME);
 62   strcat(buffter, tmp);
 63   sprintf(tmp, "Connection: Keep-Alive\r\n");
 64   strcat(buffter, tmp);
 65   sprintf(tmp, "Content-Type: %s\r\n", mime_type);
 66   strcat(buffter, tmp);
 67   sprintf(tmp, "Content-Length: %d\r\n", (int)length);
 68   strcat(buffter, tmp);
 69   sprintf(tmp, "Content-Language: zh-CN\r\n");
 70   strcat(buffter, tmp);
 71   strcat(buffter, "\r\n");
 72   return buffter;
 73 }
 74 int main(int argc, char const *argv[])
 75 {
 76   char *wwwroot = "/wwwroot/public/";
 77   struct sockaddr_in cliaddr;
 78   int socketd = socket(AF_INET, SOCK_STREAM, 0); 
 79   if (socketd == -1)
 80   {
 81     printf("%s\n", "socket is init error");
 82     exit(0);
 83   }
 84   struct sockaddr_in addr;
 85   addr.sin_family = AF_INET;
 86   addr.sin_port = htons(19019);
 87   addr.sin_addr.s_addr = htonl(INADDR_ANY);
 88 
 89   if (-1 == bind(socketd, (struct sockaddr*)&addr, sizeof(addr)))
 90   {
 91     printf("%s\n", "bind is error");
 92     exit(0);
 93   }
 94   if (-1 == listen(socketd, 5))
 95   {
 96     printf("%s\n", "listen 19019 port error");
 97     exit(0);
 98   }
 99   char buffer[BUFFSIZE];
100   char method[BUFFSIZE];
101   char path[BUFFSIZE];
102   char protocol[BUFFSIZE];
103   char resCon[2048000];
104   FILE *fp;
105   while(1)
106   {
107     socklen_t len=sizeof(struct sockaddr_in);
108     int client = accept(socketd, (struct sockaddr*)&cliaddr, &len); 
109     if(read(client, buffer, BUFFSIZE) != -1)
110     {
111      
112       if (chdir(wwwroot) < 0){
113         printf("chdir error\n");
114         exit(0);
115       }
116       sscanf(buffer, "%[^ ] %[^ ] %[^ ]", method, path, protocol);
117 
118       printf("%s\n", path);
119       if (fp = fopen((path + 1), "rb")){
120         int ich;
121         int  i=0;
122         char buff[1024000];
123         memset(buff, 0, sizeof(buff));
124         while ((ich = getc(fp)) != EOF )
125           buff[i++] = ich;
126         fclose(fp);
127         strcpy(resCon, send_header(200, "OK", get_mime_type((path + 1)), strlen(buff), resCon));
128         // strcat(resCon, tmp);
129         strcat(resCon, buff);
130         printf("%s\n", resCon);
131         write(client, resCon, sizeof(resCon));
132       }
133       
134       memset(resCon, 0, sizeof(resCon)); 
135     }
136     close(client);
137   }
138   close(socketd);
139   return 0;
140 }

 

以上代码可以返回html静态页面

返回不了图片

 Connection:Keep-Alive
Content-Language:zh-CN
Content-Length:4
Content-Type:image/jpeg
Date:Fri, 12 Apr 2013 11:50:13 GMT
Server:CFWS

这个是返回图片的head信息

求大侠指点一下啊

山鹰-Chian的主页 山鹰-Chian | 初学一级 | 园豆:4
提问于:2013-04-12 22:14
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册