Results 1 to 7 of 7

Thread: Does any1 know how to use sendfile() (in C) ?

  1. #1
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Does any1 know how to use sendfile() (in C) ?

    Why doesn't this work??

    Code:
    ...
    n = sendfile(socket_fd,file_fd, NULL, 255);
    Code:
    [raul@horus myserver]$ ./myclient nexus.rnl 40000
    Trying to connect
    Connected to server
    ERROR writing to socket: Bad file descriptor
    I can use read and write, but I wanted to use sendfile for cleaner code
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  2. #2
    Join Date
    Aug 2005
    Location
    san francisco
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Does any1 know how to use sendfile() (in C) ?

    Ah, in Linux that won't work. From "man sendfile":

    Presently (Linux 2.6.9): in_fd, must correspond to a file which sup‐
    ports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd
    must refer to a socket.

    Applications may wish to fall back to read(2)/write(2) in the case
    where sendfile() fails with EINVAL or ENOSYS.
    My guess: sendfile() does everything inside the kernel, which in this case isn't very secure, while read/write make userspace->kernel trips each time, so they don't allow it. But I'm not sure.

    EDIT: hmm, but the order of arguments is:

    Code:
    ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
    so then as I understand it, that should work? What return value do you get?
    Last edited by gradedcheese; November 9th, 2007 at 06:15 AM.

  3. #3
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Re: Does any1 know how to use sendfile() (in C) ?

    Sorry for the late response, i've been away. It just returns -1. The error message is the one on the original post.

    I'm just using normal read and write, but with sendfile i wouldn't have to create a buffer with a limited size and do several readings. I think...
    Last edited by raul_; November 10th, 2007 at 07:58 PM.
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  4. #4
    Join Date
    Aug 2005
    Location
    san francisco
    Beans
    Hidden!
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Does any1 know how to use sendfile() (in C) ?

    Well, for what it's worth, -1 is generally:

    Code:
    #define EPERM            1      /* Operation not permitted */
    ...so, just a guess: try running your test code with 'sudo'? What port are you using to communicate over?

  5. #5
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Re: Does any1 know how to use sendfile() (in C) ?

    It's not a permission problem =\ i'm using 40000 but that's just arbitrary. I guess i'll stick to the standard write and read procedures.

    When I get sendfile working i'll post it here
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  6. #6
    Join Date
    Feb 2010
    Beans
    36

    Smile Re: Does any1 know how to use sendfile() (in C) ?

    i am new to socket programming and need to build a ftp server how to recieve file at the client if i use sendfile().
    thnx in advance

  7. #7
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Does any1 know how to use sendfile() (in C) ?

    Quote Originally Posted by rejuvenate View Post
    i am new to socket programming and need to build a ftp server how to recieve file at the client if i use sendfile().
    thnx in advance
    Welcome to the wooorld of tomorrooooww!

    This post is over two years old, so you'll probably want to start a new thread. Although I commend you greatly for using the search feature so efficiently. =]
    Programming is an art. Learn it. Live it. Love it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •