sendfile |
Prototype: |
#include <unistd>
int sendfile(int out_fd, int in_fd, off_t *offset,
size_t count);
|
General Description: |
A fast way to transmit a file through a socket. The call reads the data from in_fd and writes it out to out_fd. The call does not change the file pointer in in_fd, but out_fd's file pointer is changed. The call begins reading from *offset for count bytes. When done, *offset points to the byte after the last byte read. If you need to add header information, refer to the TCP_CORK option in TCP(4) to improve performance. |
Return Value: |
If successful, the call returns the total number of bytes copied. If error, the call returns -1 and sets errno to the error code. |
Parameters |
out_fd |
The destination descriptor (file pointer modified). |
in_fd |
The source descriptor (file pointer not modified). |
offset |
A pointer to a variable that holds the starting offset. |
count |
The number of bytes to send. |
Possible Errors |
EBADF |
Input file was not opened for reading or output file was not opened for writing. |
EINVAL |
Descriptor is not valid or locked. |
ENOMEM |
Insufficient memory for reading from in_fd. |
EIO |
Unspecified error while reading from in_fd. |
Examples |
#include <unistd.h>
...
struct stat fdstat;
int client = accept(sd, 0, 0);
int fd = open("filename.gif", O_RDONLY);
fstat(fd, &fdstat);
sendfile(client, fd, 0, fdstat.st_size);
close(fd);
close(client);
|