Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Received file doesnt display contents properly. (http://www.velocityreviews.com/forums/t598375-received-file-doesnt-display-contents-properly.html)

Neehar Athalye 03-13-2008 07:43 AM

Received file doesnt display contents properly.
 
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);



Richard Heathfield 03-13-2008 08:23 AM

Re: Received file doesnt display contents properly.
 
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

deepak 03-13-2008 09:06 AM

Re: Received file doesnt display contents properly.
 
On Mar 13, 12:43 pm, Neehar Athalye <neehar.atha...@gmail.com> 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);



David Thompson 03-24-2008 02:14 AM

Re: Received file doesnt display contents properly.
 
On Thu, 13 Mar 2008 08:23:42 +0000, Richard Heathfield
<rjh@see.sig.invalid> 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


All times are GMT. The time now is 11:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.