Received file doesnt display contents properly.

Discussion in 'C Programming' started by Neehar Athalye, Mar 13, 2008.

  1. Hi,

    I urgently need some help regarding my assignment. I am receiving a
    file from a Server (run on localhost)
    using TCP. I can receive files of any size upto a few megabytes.

    If I receive files of the order of 2 KB, no problem in displaying the
    contents, but if the file size is around 100 KB or more it shows
    garbled text. I verified that the client is able to receive all the
    file bytes by doing ls -l, it shows the exact file size of the
    received file as the one that exists on the server.
    I believe there is some problem with the way I am writing file from
    received bytes.
    I am opening file to be written in binary mode and using fwrite to
    write to the file.

    Please help..

    Here is my code snippet:

    /* receive file as a byte stream from server */

    while(count = recv(sockfd_client, buffer, (MAX_BUFFER_SIZE *
    sizeof(char)), 0))

    {

    if(count == -1)

    {

    perror("recv");

    exit(1);

    }



    /* this var keeps count of all the bytes received from server */

    file_size = file_size + count;

    }










    /* open a new file in binary mode to write received data */

    temp = fopen(file_name, "wb");



    /* use fwrite to write block of data at a time to file */

    fwrite(buffer, 1, (file_size - 1), temp);

    printf("\nReceived File %s \n\n", file_name);



    /* close connection with server */

    close(sockfd_client);



    /* close file descriptor */

    fclose(temp);
     
    Neehar Athalye, Mar 13, 2008
    #1
    1. Advertising

  2. Neehar Athalye said:

    > Hi,
    >
    > I urgently need some help regarding my assignment. I am receiving a
    > file from a Server (run on localhost)
    > using TCP. I can receive files of any size upto a few megabytes.
    >
    > If I receive files of the order of 2 KB, no problem in displaying the
    > contents, but if the file size is around 100 KB or more it shows
    > garbled text. I verified that the client is able to receive all the
    > file bytes by doing ls -l, it shows the exact file size of the
    > received file as the one that exists on the server.
    > I believe there is some problem with the way I am writing file from
    > received bytes.


    <snip>

    > while(count = recv(sockfd_client, buffer, (MAX_BUFFER_SIZE *
    > sizeof(char)), 0))
    >
    > {


    <snip>

    In the lines that I've snipped, there is no code for copying the data
    safely from the buffer into a holding area before writing. Assuming that
    buffer is either MAX_BUFFER_SIZE or perhaps MAX_BUFFER_SIZE + 1 bytes in
    size, this means that you don't have room for any files greater than
    MAX_BUFFER_SIZE, and only the last MAX_BUFFER_SIZE bytes of the file are
    preserved.

    > /* this var keeps count of all the bytes received from server */
    >
    > file_size = file_size + count;


    > }
    >
    >
    > /* open a new file in binary mode to write received data */
    >
    > temp = fopen(file_name, "wb");


    This can fail. Check that temp != NULL before proceeding.

    > /* use fwrite to write block of data at a time to file */
    >
    > fwrite(buffer, 1, (file_size - 1), temp);


    If you have file_size bytes, why only write file_size - 1 bytes?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 13, 2008
    #2
    1. Advertising

  3. Neehar Athalye

    deepak Guest

    On Mar 13, 12:43 pm, Neehar Athalye <> wrote:
    > Hi,
    >
    > I urgently need some help regarding my assignment. I am receiving a
    > file from a Server (run on localhost)
    > using TCP. I can receive files of any size upto a few megabytes.
    >
    > If I receive files of the order of 2 KB, no problem in displaying the
    > contents, but if the file size is around 100 KB or more it shows
    > garbled text. I verified that the client is able to receive all the
    > file bytes by doing ls -l, it shows the exact file size of the
    > received file as the one that exists on the server.
    > I believe there is some problem with the way I am writing file from
    > received bytes.
    > I am opening file to be written in binary mode and using fwrite to
    > write to the file.
    >
    > Please help..
    >
    > Here is my code snippet:
    >
    > /* receive file as a byte stream from server */
    >
    > while(count = recv(sockfd_client, buffer, (MAX_BUFFER_SIZE *
    > sizeof(char)), 0))
    >
    > {
    >
    > if(count == -1)
    >
    > {
    >
    > perror("recv");
    >
    > exit(1);
    >
    > }
    >
    > /* this var keeps count of all the bytes received from server */
    >
    > file_size = file_size + count;
    >
    > }


    Is it because you closed the bracket for while before wrinting it to
    file?
    >
    > /* open a new file in binary mode to write received data */
    >
    > temp = fopen(file_name, "wb");
    >
    > /* use fwrite to write block of data at a time to file */
    >
    > fwrite(buffer, 1, (file_size - 1), temp);
    >
    > printf("\nReceived File %s \n\n", file_name);
    >
    > /* close connection with server */
    >
    > close(sockfd_client);
    >
    > /* close file descriptor */
    >
    > fclose(temp);
     
    deepak, Mar 13, 2008
    #3
  4. On Thu, 13 Mar 2008 08:23:42 +0000, Richard Heathfield
    <> wrote:

    > Neehar Athalye said:


    > > If I receive files of the order of 2 KB, no problem in displaying the
    > > contents, but if the file size is around 100 KB or more it shows
    > > garbled text. I verified that [...] the [written] file size [is correct]


    > > while(count = recv(sockfd_client, buffer, (MAX_BUFFER_SIZE *
    > > sizeof(char)), 0))
    > >
    > > {

    >
    > <snip>
    >
    > In the lines that I've snipped, there is no code for copying the data
    > safely from the buffer into a holding area before writing. Assuming that
    > buffer is either MAX_BUFFER_SIZE or perhaps MAX_BUFFER_SIZE + 1 bytes in
    > size, this means that you don't have room for any files greater than
    > MAX_BUFFER_SIZE, and only the last MAX_BUFFER_SIZE bytes of the file are
    > preserved.
    >

    More likely only *the last TCP segment modulo MAX_BUFFER_SIZE*
    overlaid on previous data which may be the earlier part of the last
    segment, or part(s) of the next to last segment, depending on size.
    Depending on the TCP stack, the maximum segment size (MSS) is likely
    to be only a few KB, which is consistent with what the OP is getting.
    <nonstandard> socket recv </> does not try to always do a 'full' read
    like <standard> fread </> does for files.

    The OP needs to do something more like:
    sofar = 0;
    while( sofar < SIZE &&
    (count = recv (sd, buffer+sofar, SIZE-sofar, fl)) != 0 ){
    if( count < 0 ) error;
    sofar += count;
    }
    ... use buffer for sofar bytes ...
    or (my preference):
    sofar = 0;
    while( sofar < SIZE &&
    (count = recv (sd, buffer+sofar, SIZE-sofar, fl)) > 0 )
    sofar += count;
    if( count < 0 ) error;
    ... as above ...

    Actually you can skip the sofar<SIZE precheck if you then allow for
    count==0 either because of 'EOF' OR because you requested zero,
    but that's finicky to get right and even worse to explain/comment.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Mar 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWxleCBGaW1pbmU=?=

    IE6 doesnt render the page properly whereas IE7 does

    =?Utf-8?B?QWxleCBGaW1pbmU=?=, Aug 31, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    409
    Patrice
    Aug 31, 2007
  2. Replies:
    3
    Views:
    443
    alex23
    May 27, 2008
  3. kishore
    Replies:
    7
    Views:
    2,102
    kishore
    Mar 10, 2010
  4. Aaron Abdis

    Inherited Web Label doesnt properly initialize "Text" property

    Aaron Abdis, Aug 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    137
    Aaron Abdis
    Aug 19, 2004
  5. Replies:
    0
    Views:
    204
Loading...

Share This Page