#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>

#define BUFSIZE	4096

#define DGRAM	"Datagram for Mongo.\n"

/*
 #define UNIX_PATH_MAX    108

 struct sockaddr_un {
   sa_family_t sun_family;		// AF_UNIX
   char sun_path[UNIX_PATH_MAX];	// pathname
 };
*/

int main(int argc, char *argv[])
{
  struct sockaddr_un myname, name;
  char *mypath = "/tmp/client_sock";
  char buf[1024], *path = "/tmp/serv_sock";
  int r;

  if (argc > 1) path = argv[1];

  /* Create socket, initially un-connected. */
  int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket");
    exit(1);
  }

  /* Create name (filesystem socket file) */
  myname.sun_family = AF_UNIX;
  myname.sun_path[0] = 0;
//  strcpy(myname.sun_path, mypath);
  /* Remove stale socket file if present */
//  unlink(mypath);
  /* Bind socket to name: */
  if (bind(sock, (struct sockaddr *)&myname, sizeof(sa_family_t))) {
    perror("bind");
    exit(1);
  }

  /* Define name (filesystem socket file) to send to: */
  name.sun_family = AF_UNIX;
  strcpy(name.sun_path, path);

  if (sendto(sock, DGRAM, sizeof(DGRAM), 0, (struct sockaddr *)&name,
      sizeof(struct sockaddr_un)) < 0) {
    perror("sendto");
    exit(1);
  }
  r = read(sock, buf, sizeof(buf));
  if (r) printf("%.*s", r, buf);
  close(sock);
  return 0;
}
