sending image over HTTP1.0 only half of image sent

209 Views Asked by At

I am doing a class project right now and it requires me to send data and image using socket. So far I have finished most of it. Data gets sent correctly, small images get sent correctly, but when it comes to large images, only half of it is shown. I checked the characters sent and all of them got sent. So I am not sure where I got wrong. This my function to handle get request. I have tested sending data and it worked, so I think most of this function works but probably just some small details I missed. Please help...thanks!

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

There are 0 best solutions below