read(file, tmp_buf, len);
write(socket, tmp_buf, len);
read(file, tmp_buf, len);
write(socket, tmp_buf, len);
tmp_buf = mmap( file , len);
write( socket , tmp_buf , len);
if( fcntl( fd , F_SETSIG , RT_SIGNAL_LEASE) == - 1) {
perror( "kernel lease set signal");
return - 1;
}
/* l_type can be F_RDLCK F_WRLCK */
if( fcntl( fd , F_SETLEASE , l_type )){
perror( "kernel lease set type");
return - 1;
}
#include <sys/sendfile.h>
ssize_t sendfile( int out_fd , int in_fd , off_t * offset , size_t count);
在我的实验中,但存用 write()/read() 拷贝一个 1G 的数据,和用 sendfile() 拷贝同样大小的数据,并未见有什么性能和时间上的优势。
原型:
#include <sys/sendfile.h>
ssize_t sendfile( int out_fd , int in_fd , off_t * offset , size_t count);
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main ( int argc , char ** argv)
{
struct sockaddr_un sin1;
int server_sockfd , client_sockfd;
int server_len , client_len;
ssize_t bytes , res = 0;
ssize_t rtotal = 0;
FILE * stream;
int in_fd;
struct stat buf;
off_t off = 0;
unlink ( "server_socket");
unlink ( "src_sendfile_save");
stream = fopen ( "src_sendfile_save" , "w");
if ( ! stream) {
perror ( "fopen");
exit ( EXIT_FAILURE);
}
fclose ( stream);
if (( in_fd = open ( "src" , O_RDONLY)) < 0) {
printf ( "Can't open 'src' file");
exit ( EXIT_FAILURE);
}
if ( fstat ( in_fd , & buf) == - 1) {
printf ( "Can't stat 'src' file \n ");
exit ( EXIT_FAILURE);
}
printf ( "Get file size are %u bytes \n " , buf . st_size);
server_sockfd = socket ( AF_UNIX , SOCK_STREAM , 0);
if ( server_sockfd < 0) {
perror ( "socket");
exit ( EXIT_FAILURE);
}
sin1 . sun_family = AF_UNIX;
strcpy ( sin1 . sun_path , "server_socket");
server_len = sizeof ( sin1);
if ( bind ( server_sockfd , ( struct sockaddr *) & sin1 , server_len) < 0) {
perror ( "bind");
exit ( EXIT_FAILURE);
}
if ( listen ( server_sockfd , 5) < 0) {
perror ( "listen");
exit ( EXIT_FAILURE);
}
printf ( "The server is waiting for client connect... \n ");
client_sockfd = accept ( server_sockfd , ( struct sockaddr *) & sin1 , ( socklen_t *) & client_len);
if ( client_sockfd == - 1 ) {
perror ( "accept");
exit ( EXIT_FAILURE);
}
while ( off < buf . st_size) {
if (( res = sendfile ( client_sockfd , in_fd , & off , buf . st_size)) < 0 ) {
printf ( "sendfile failed \n ");
exit ( EXIT_FAILURE);
} else {
rtotal += res;
}
}
printf ( "server sendfile total %u bytes \n " , rtotal);
close ( client_sockfd);
unlink ( "server_socket");
return ( 0);
}
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main ( int argc , char * argv [])
{
struct sockaddr_un address;
int sockfd;
int len , result;
int i , bytes;
struct stat buf;
off_t off;
ssize_t res , total = 0;
int wfd;
char rwbuf [ 4096 ];
wfd = open ( "src_sendfile_save" , O_WRONLY);
if ( wfd < 0) {
perror ( "open");
exit ( EXIT_FAILURE);
}
/*..socket,AF_UNIX....,SOCK_STREAM....*/
if (( sockfd = socket( AF_UNIX , SOCK_STREAM , 0)) == - 1) {
perror ( "socket");
exit ( EXIT_FAILURE);
}
address . sun_family = AF_UNIX;
strcpy ( address . sun_path , "server_socket");
len = sizeof ( address);
/*..........*/
result = connect ( sockfd , ( struct sockaddr *) & address , len);
if ( result == - 1) {
printf ( "ensure the server is up \n ");
perror ( "connect");
exit ( EXIT_FAILURE);
}
while (( res = read ( sockfd , rwbuf , 4096)) > 0) {
total += res;
write ( wfd , rwbuf , 4096);
}
printf ( "total %u bytes received from server snedfile \n " , total);
close ( sockfd);
close ( wfd);
return ( 0);
}