通过HTTP1.0发送图像只发送一半图像

时间:2015-04-30 20:31:06

标签: c image sockets http http-1.0

我正在做一个类项目,它需要我使用socket发送数据和图像。到目前为止,我完成了大部分工作。数据被正确发送,小图像被正确发送,但是当涉及到大图像时,只显示了一半。我检查了发送的字符,并且所有字符都已发送。所以我不确定我哪里出错了。这是我处理get请求的函数。我已经测试了发送数据并且它有效,所以我认为这个功能的大部分都可以工作,但可能只是我错过的一些小细节。请帮忙......谢谢!

void handle_get(int sock, const char* url) {
   FILE* pFILE; 
   int file, i;
   //file = open(url, O_RDONLY);
   pFILE = fopen(++url, "r");
   char response_message[1024];
   char buffer[1024];
   char c;
   char *header_type, *file_type;
   long file_size;
   //char *buffer;

   if (pFILE == NULL){
    snprintf (response_message, sizeof (response_message), not_found_response_template, url);
        write (sock, response_message, strlen (response_message));
        error("ERROR opening requested file");
   }

   file_type = strrchr (url, '.'); 
   //return pointer to the last occurrance of '.'
    if (!file_type)
        file_type = "html";
    else
    file_type++;

    if (strcasecmp (file_type, "jpg") == 0 || strcasecmp (file_type, "jpeg") == 0)
        header_type = "image/jpeg";
    else if (strcasecmp (file_type, "gif") == 0)
        header_type = "image/gif";
    else if (strcasecmp (file_type, "png") == 0)
        header_type = "image/png";
    else
        header_type = "text/html";

    snprintf (response_message, sizeof (response_message), ok_response, header_type);
    write (sock, response_message, strlen (response_message));
    puts(response_message);

    int n = 0;
    bzero(buffer, 1024);
    while (n=fread(buffer, sizeof(char), 1024, pFILE)){
        printf("n is %d\n", n);
        send (sock, buffer, n, 0);
        //write(sock, buffer, n);
        bzero(buffer, 1024);
    }

    fclose (pFILE);
}

0 个答案:

没有答案