Received file doesnt display contents properly.

N

Neehar Athalye

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);
 
R

Richard Heathfield

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.

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?
 
D

deepak

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?
 
D

David Thompson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top