Originally Posted by
CommonTater
First off you shouldn't be writing sizeof(buffer) you should be writing numbytes after receiving a packet. The packet might be shorter than the buffer, so if you write the whole thing it will get corrupted.
Third the loop should not exit on numbytes = 0 ... if your sender is working properly every block of the file will be a full buffer long and only the last block will be shorter so you want to exit your loop when numbytes < buffersize.
With buffersize do you mean sizeof(buffer) so 1024? Because when I do it like this:
Code:
do {
//Receiving From Socket
numbytes = recv(sockfd, buffer, sizeof(buffer), 0);
//Counting Bytes Received
count = count + numbytes;
/* if (count >= filesize)
{
printf("\n:>File receive Completed\n");
closesocket(sockfd);
fclose(fp);
}
else
{*/
if (bolRecvFile == false)
{
printf(":>Receiving file\n");
bolRecvFile = true;
}
fwrite(buffer, numbytes, 1, fp);
//}
}while(numbytes < 1024);
This code works:
Code:
do {
//Receiving From Socket
numbytes = recv(sockfd, buffer, sizeof(buffer), 0);
//Counting Bytes Received
count = count + numbytes;
/* if (count >= filesize)
{
printf("\n:>File receive Completed\n");
closesocket(sockfd);
fclose(fp);
}
else
{*/
if (bolRecvFile == false)
{
printf(":>Receiving file\n");
bolRecvFile = true;
}
fwrite(buffer, numbytes, 1, fp);
//}
}while(count < filesize);
if (count >= filesize)
{
printf("\n:>File receive Completed\n");
closesocket(sockfd);
fclose(fp);
}