#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h># Include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include < string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ Sendfile.h>int Main () {const char *ip= "127.0.0.1"; int port=12345; const char *file_name= "MAIN.C"; int Filefd=open (file_name,o_rdonly); ASSERT (FILEFD>0); struct stat stat_buf; Fstat (FILEFD,&STAT_BUF); struct SOCKADDR_IN address; Bzero (&address,sizeof (address)); Address.sin_family=af_inet; Inet_pton (AF_INET,IP,&ADDRESS.SIN_ADDR); Address.sin_port=htons (port); int Sock=socket (pf_inet,sock_stream,0); ASSERT (SOCK>0); int Ret=bind (sock, (struct sockaddr *) &address,sizeof (address)); ASSERT (Ret!=-1); Ret=listen (sock,5); struct SOCKADDR_IN client; socklen_t client_addrlength=sizeof (CLIENT); int connfd=accept (sock, (struct sockaddr *) &client,&client_addrlength); if (connfd<0) {printf ("Errorno is:%d", errno); } else {sendfile (connfd,filefd,null,stat_buf.st_size); Close (CONNFD); } close (sock); return 0;}
Using the Sendfile function to transfer files under Linux