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