我已经编写了这段代码,它将文本文件的内容读入缓冲区并通过套接字发送缓冲区直到文件结束。
代码工作正常,在通过套接字发送完整文件后, fread() 失败,但是,根据我在通过套接字发送完整文件时的理解,由于条件 (while(fpSend!=NULL)),fread () 甚至不能被称为:(
请查看代码,如果您发现任何错误,请告诉我:(
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);
}
/*MessageBox( NULL,
file_buffer,
"File copied in Buffer",
MB_ICONEXCLAMATION |
MB_OK);*/
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);
//exit(1);
}
else
{
//sent
}
}
MessageBox( NULL,
"File Sent Successfully!",
"SENT!",
MB_ICONEXCLAMATION |
MB_OK);
fclose(fpSend);
}