client2-sendfile.c
#include <sys/un.h>       /* struct sockaddr_un                    */
#include <sys/socket.h>   /* socket, connect, AF_UNIX, SOCK_STREAM */
#include <sys/stat.h>     /* fstat                                 */
#include <sys/fcntl.h>    /* open, O_RDONLY, struct stat           */
#include <sys/sendfile.h> /* sendfile                              */
#include <stdio.h>        /* printf, perror                        */
#include <unistd.h>       /* read, write, close, STDIN_FILENO      */
#include "socktest.h"     /* shared defines                        */

int main(int argc, char **argv)
{
  int sfd;                       /* socket file descriptor         */
  int fd;                        /* file descriptor (open file)    */
  ssize_t bytes_read;            /* count of bytes read from stdin */
  struct stat statbuf;           /* info from stat call            */
  struct sockaddr_un addr = {0}; /* zero out structure             */

  if (argc < 2)
  {
    printf("Usage: %s filename\n", argv[0]);
    return 1;
  }

    /* Create the socket */
  sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sfd == -1)
  {
    perror("socket");
    return 2;
  }

    /* Set domain type */
  addr.sun_family = AF_UNIX;

    /* Set pathname (safely) */
  strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);

    /* Connect socket to pathname */
  if (connect(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1)
  {
    perror("connect");
    return 3;
  }
  
  /************************ New stuff *************************/  

    /* Open the file for read-only. */
  fd = open(argv[1], O_RDONLY);
  if (fd == -1)
  {
    perror("open");
    return 4;
  }

    /* Get metadata info (e.g. filesize) */
  if (fstat(fd, &statbuf) == -1)
  {
    perror("fstat");
    return 5;
  }

    /* Send the entire file */
  bytes_read = sendfile(sfd, fd, NULL, statbuf.st_size);
  if (bytes_read == -1)
  {
    perror("sendfile");
    return 6;
  }

  close(fd);  /* Close the file   */

  /************************************************************/  

  close(sfd); /* Close the socket */

  return 0; 
}