#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>

int                 socketUn;
struct sockaddr_un  addrUn;
int                 lenUn;
int                 size = 1;
char                datagram[2000];

void sockInit();

int main() {
  sockInit();

  while(1) {
    sleep(30);

    while(size != -1){
      size = recvfrom(socketUn, &datagram, sizeof(datagram), 0,
                      (struct sockaddr*)NULL, (socklen_t *)NULL);
    /*  if (size == -1) perror("recvfrom failed: ");
        else printf("DATAGRAM: size: %d\n", size);*/
    }
    size = 0;
  }
}


void sockInit() {
  if ( unlink("/tmp/tempUn") == -1) perror("unlink failed\n");

  socketUn = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (socketUn == -1) perror("socket failed");

  addrUn.sun_family = AF_UNIX;

  strcpy(addrUn.sun_path, "/tmp/tempUn");

  lenUn = strlen(addrUn.sun_path) + sizeof(addrUn.sun_family);

  if ( bind(socketUn, (struct sockaddr *)&addrUn, lenUn) == -1)
    perror("bind failed");

  fcntl(socketUn, F_SETFL, O_APPEND|O_NONBLOCK);
}

