I have written this code which reads contents of text file into a buffer and sends the buffer over the socket until end of file.
The code works fine and after complete file is sent to over the socket, fread() fails, however, according to my undersanding when complete file is sent over the socket, due to the condition (while(fpSend!=NULL)), fread() must not be called even :(
Please have a look at the code and do let me know if you see any mistake :(
void sendingFile()
{
FILE *fpSend ;
if((fpSend = fopen("Client0.txt", "r+b")) == NULL)
{
MessageBox( NULL,
"Unable to open the File",
"Error!",
MB_ICONEXCLAMATION |
MB_OK);
exit(EXIT_FAILURE);
}
char file_buffer[2000];
fseek(fpSend, 0, SEEK_END);
size_t file_size = ftell(fpSend);
fseek(fpSend, 0, SEEK_SET);
while(fpSend!=NULL)
{
int bytes_read=0;
if((bytes_read=fread(file_buffer, 1,12, fpSend))<=0)
{
char err[128], bread[128];
itoa(errno,err,10);
itoa(bytes_read,bread,10);
MessageBox( NULL,
"Unable to copy file into buffer",
bread,
MB_ICONEXCLAMATION |
MB_OK);
exit(1);
}
if(sendto(socketIdentifier, file_buffer, bytes_read, 0, (struct sockaddr *) &AH_glb_connectedSocket, sizeof(AH_glb_connectedSocket))<0)
{
MessageBox( NULL,
" NOT SENNT!",
"ERROR!",
MB_ICONEXCLAMATION |
MB_OK);
}
else
{
}
}
MessageBox( NULL,
"File Sent Successfully!",
"SENT!",
MB_ICONEXCLAMATION |
MB_OK);
fclose(fpSend);
}