1

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);
}
santoki3
  • 83
  • 1
  • 7
  • I also noticed that if I tried to send the image twice by manually setting up for loop that runs twice. the image shows up correctly....i have no idea why this would happen either. – santoki3 Apr 30 '15 at 20:33
  • Don't update your questions by sending comments. Edit your question directly. – Jabberwocky Apr 30 '15 at 20:35
  • 1
    Most images are *binary* files, you're opening the image file as a *text* file. That means that depending on platform the `fread` function might do some conversion of certain "character" sequences, like newline to carriage-return and newline, or the opposite. – Some programmer dude Apr 30 '15 at 20:36
  • 1
    Also, what is `ok_response`? Does it contain a `Content-lenght` header (it doesn't seem like that)? How large is the image you're sending? What is the output of this function (more specifically, if you add all the reads, what's the total length read)? And in your actual code, you *do* check for errors? – Some programmer dude Apr 30 '15 at 20:37
  • Yes i have done all of the checking and they all work correctly. ok response doesnt include content-length originally. i just added that and it is still the same. the image i am sending ranges from 40 KB to 2 MB. 40KB works fine. anything larger than 300KB start to show partially – santoki3 Apr 30 '15 at 20:53
  • I have checked that if I add all the reads it is the same length as the original file – santoki3 Apr 30 '15 at 20:54
  • 1
    Is the client a third-party web browser, or is it also custom-written? If the latter, what makes you confident the problem is on the server side? – John Bollinger Apr 30 '15 at 20:57
  • it is a third party web browser. im using firefox – santoki3 Apr 30 '15 at 20:59
  • And if you add all returns from `send` (you *do* check what `send` returns, right?) does it also add up to the full file total? Also, some systems have a "sendfile" system call, that can be used to "send" a file, for example [here's the manual page for the Linux `sendfile` system call](http://man7.org/linux/man-pages/man2/sendfile.2.html), you might think about using something like that. – Some programmer dude May 01 '15 at 01:12

0 Answers0