home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_12 / 8n12113b < prev    next >
Text File  |  1990-07-25  |  3KB  |  101 lines

  1. /*
  2.  *  server.c
  3.  *
  4.  *    Server process for time accounting system, non-network version.  
  5.  *    This should be run as a daemon or in the background.
  6. */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <netdb.h>
  12. #include <stdio.h>
  13. #include "time_entry.h"
  14.  
  15. main()
  16. {
  17.  
  18.     int skt_id,    /* Socket descriptor */
  19.         msg_sock,  /* Socket that will accept messages */
  20.         i,j;
  21.  
  22.     EVENT_TYPE event;    /* An event from one of the client processes */
  23.  
  24.     struct sockaddr sa;  /* Socket address structure   */
  25.     char buff[80];
  26.     FILE *time_file;
  27.  
  28.         /* Open the output file in append mode */
  29.         if(!(time_file = fopen(TIME_FILE, "a+"))){
  30.             perror("Opening time file");
  31.             exit(1);
  32.         }
  33.  
  34.         fprintf(time_file, "\n***STARTUP***\n");
  35.  
  36.         /*
  37.          *  Create a stream type socket in the UNIX domain, with the 
  38.          *  default protocol.
  39.         */
  40.         skt_id = socket(AF_UNIX, SOCK_STREAM, 0);
  41.         if(skt_id == -1){
  42.             /* Error.  Cleanup & leave */
  43.             perror("Can't create socket");
  44.             exit(1);
  45.         }
  46.  
  47.         /*
  48.          *  Now that the socket exists, set up the address structure
  49.          *  and bind an address to the socket.
  50.         */
  51.         sa.sa_family = AF_UNIX;
  52.         strcpy(sa.sa_data, SOCKET_NAME);
  53.  
  54.         if(bind(skt_id, &sa, sizeof(sa)) == -1){
  55.             perror("Can't bind address");
  56.             exit(1);
  57.         }
  58.  
  59.         listen(skt_id, 5);  /* Set up the queue for requests */
  60.  
  61.  
  62.         /*
  63.          *  Main loop is here.  Accept a connection, read the message,
  64.          *  and write the time record...
  65.         */
  66.  
  67.         do{
  68.             /*
  69.              * Accept a connection.  Accept will return a new socket
  70.              * descriptor with the same properties as the original.  This
  71.              * new socket is used for read and write
  72.             */
  73.  
  74.         msg_sock = accept(skt_id, 0, 0);
  75.         if(msg_sock == -1){
  76.             perror("Can't accept connections");
  77.             exit(1);
  78.         }
  79.             if(read(msg_sock, &event, sizeof(event)) < 0){
  80.                 perror("Read");
  81.                 exit(1);
  82.             }
  83.             if(event.event_cd != SHUT_DOWN){
  84.                 if (fwrite(&event, sizeof(event),1,time_file) != 1){
  85.                     perror("Write");
  86.                     exit(1);
  87.                 }
  88.             }
  89.             close(msg_sock);  /* Close the socket retruned by accept... */
  90.         }while(event.event_cd != SHUT_DOWN);
  91.  
  92.         /* Cleanup */
  93.         unlink(SOCKET_NAME);
  94.         fprintf(time_file, "\n***SHUTDOWN***\n");
  95.         fclose(time_file);
  96.         exit(0);
  97. }
  98.  
  99.  
  100.  
  101.