How would you receive a file sent with 'sendfile'?
I'm trying to implement a basic file server. I have been trying to use the sendfile command found here: http://linux.die.net/man/2/sendfile I'm using TCP.
I can have it send fine, but its sending in binary and I'm not sure if thats the hang up.
I am trying to receive the file with recv, but it isn't coming through correctly. Is there a special way to r开发者_开发技巧eceive a binary file, and put it into a string?
EDIT: Asked to supply some code, here it is:
SENDFILE Call (from Server process)
FILE * file = fopen(filename,"rb");
if ( file != NULL)
{
/*FILE EXISITS*/
//Get file size (which is why we opened in binary)
fseek(file, 0L, SEEK_END);
int sz = ftell(file);
fseek(file,0L,SEEK_SET);
//Send the file
sendfile(fd,(int)file,0,sz);
//Cleanup
fclose(file);
}
RECIEVE Call (from Client process, even more basic than a loop, just want a single letter)
//recieve file
char fileBuffer[1000];
recv(sockfd,fileBuffer,1,0);
fprintf(stderr,"\nContents:\n");
fprintf(stderr,"%c",fileBuffer[0]);
EDIT: wrote some code for checking return values. sendfile is giving errno 9 - bad file number. Which im assuming is at my second file descriptor in the call (the one for the file i'm sending). I cast it as an int because sendfile was complaining it wasn't an int.
How should I use send file given the file pointer code I have used above in th sendfile call?
You cannot use sendfile()
with a FILE*
, you need a file descriptor as given by open()
, close()
and friends. You cannot just cast a FILE* into an int and thinking it would work.
Maybe you should read the sendfile()
manpage for more information.
There is no special way. You just receive with read()
or recv()
.
Probably, you've got your receiving code wrong.
精彩评论