Source and Usage
You can download the full source code from ftp.ssc.com/pub/lj/listings/issue105/6345.tgz. Type tar -zxvf sfl-src.tgz to unpack it and make to build the executable. You should have an executable file called sfl in your directory; if you start it without any parameters, it will display usage. You have to specify the server's IP address in both server and client instances of the sfl command. Here is a use case:
Sender:
visitor@xalien-saucer:~/sfl-src > ./sfl s 10.0.0.2 Server binding to [10.0.0.2] Server sent 10240 bytes. visitor@xalien-saucer:~/sfl-src >
Receiver:
visitor@earth:~/sfl-src > ./sfl r 10.0.0.2 Client connecting to [10.0.0.2] Client received 10240 bytes. visitor@earth:~/sfl-src >
Code Comments
1. I used port 1033; use a different port if there is a conflict on your system.
2. To make the example code shorter and simpler, I put the 10K buffer on the stack. You should use malloc to allocate buffers in your code.
3. To make the code shorter for the article, I skipped checking the return value of the close system call. In a real-world program you should definitely check the return value.
Examples
If you are interested in learning more about the usage of the sendfile system call, take a look at some of the applications that use it: Apache (www.apache.org), Samba (www.samba.org), Mozilla (www.mozilla.org) and Pure-FTPd (pureftpd.sf.net).
To learn more about kernel file leases, take a look at Samba code, in the file smbd/oplock_linux.c.