您将如何接收使用“sendfile”发送的文件?

发布于 2024-12-07 10:52:54 字数 1130 浏览 0 评论 0原文

我正在尝试实现一个基本的文件服务器。我一直在尝试使用此处找到的 sendfile 命令: http://linux.die.net/man /2/sendfile 我正在使用 TCP。

我可以让它发送得很好,但它以二进制形式发送,我不确定这是否是挂断的原因。

我正在尝试使用recv 接收文件,但它没有正确接收。 有没有一种特殊的方法来接收二进制文件并将其放入字符串中?

编辑: 要求提供一些代码,这里是:

SENDFILE 调用(来自服务器进程)

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 调用(来自客户端进程,甚至比循环更基本,只需要一个字母)

//recieve file
    char fileBuffer[1000];
    recv(sockfd,fileBuffer,1,0);
    fprintf(stderr,"\nContents:\n");
    fprintf(stderr,"%c",fileBuffer[0]);

编辑:编写了一些用于检查返回值的代码。 sendfile 给出 errno 9 - 错误的文件号。我假设是在调用中的第二个文件描述符(我正在发送的文件的文件描述符)。我将其转换为 int,因为 sendfile 抱怨它不是 int。

考虑到我在上面的 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 receive 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-12-14 10:52:54

您不能将 sendfile()FILE* 一起使用,您需要由 open()close() 给出的文件描述符和朋友们。您不能只是将 FILE* 转换为 int 并认为它会起作用。

也许您应该阅读 sendfile() 联机帮助页以获取更多信息。

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.

楠木可依 2024-12-14 10:52:54

没有什么特别的办法。您只需使用 read()recv() 即可接收。

可能是您的接收代码错误。

There is no special way. You just receive with read() or recv().

Probably, you've got your receiving code wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文