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

  1. /*
  2.  *  net_server.c
  3.  *
  4.  *    Server process for time accounting system, 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.         length,
  21.         i,j;
  22.  
  23.     EVENT_TYPE event;    /* An event from one of the client processes */
  24.  
  25.     struct sockaddr_in sa;  /* Socket address structure   */
  26.     char buff[80];
  27.     FILE *time_file;
  28.  
  29.         /* Open the output file in append mode */
  30.         if(!(time_file = fopen(TIME_FILE, "a+"))){
  31.             perror("Opening time file");
  32.             exit(1);
  33.         }
  34.  
  35.         fprintf(time_file, "\n***STARTUP***\n");
  36.  
  37.         /*
  38.          *  Create a stream type socket in the INTERNET domain, with the 
  39.          *  default protocol.
  40.         */
  41.         skt_id = socket(AF_INET, SOCK_STREAM, 0);
  42.         if(skt_id == -1){
  43.             /* Error.  Cleanup & leave */
  44.             perror("Can't create socket");
  45.             exit(1);
  46.         }
  47.  
  48.         /*
  49.          *  Now that the socket exists, set up the address structure
  50.          *  and bind an address to the socket.
  51.         */
  52.         sa.sin_family = AF_UNIX;
  53.         sa.sin_addr.s_addr = INADDR_ANY;   /* Any available address */
  54.         sa.sin_port=0;                     /* Any port              */
  55.  
  56.         if(bind(skt_id, &sa, sizeof(sa)) == -1){
  57.             perror("Can't bind address");
  58.             exit(1);
  59.         }
  60.  
  61.         /*
  62.          *  Find out the port number, since we asked for any available
  63.         */
  64.  
  65.         length=sizeof(sa);
  66.         if(getsockname(skt_id, &sa, &length)){
  67.             perror("Can't find name");
  68.             exit(1);
  69.         }
  70.         printf("Port: %d\n", ntohs(sa.sin_port));
  71.  
  72.         listen(skt_id, 5);  /* Set up the queue for requests */
  73.  
  74.  
  75.         /*
  76.          *  Main loop is here.  Accept a connection, read the message,
  77.          *  and write the time record...
  78.         */
  79.  
  80.         do{
  81.             /*
  82.              * Accept a connection.  Accept will return a new socket
  83.              * descriptor with the same properties as the original.  This
  84.              * new socket is used for read and write
  85.             */
  86.  
  87.         msg_sock = accept(skt_id, 0, 0);
  88.         if(msg_sock == -1){
  89.             perror("Can't accept connections");
  90.             exit(1);
  91.         }
  92.             if(read(msg_sock, &event, sizeof(event)) < 0){
  93.                 perror("Read");
  94.                 exit(1);
  95.             }
  96.             if(event.event_cd != SHUT_DOWN){
  97.                 if (fwrite(&event, sizeof(event),1,time_file) != 1){
  98.                     perror("Write");
  99.                     exit(1);
  100.                 }
  101.             }
  102.             close(msg_sock);  /* Close the socket retruned by accept... */
  103.         }while(event.event_cd != SHUT_DOWN);
  104.  
  105.         /* Cleanup */
  106.         unlink(SOCKET_NAME);
  107.         fprintf(time_file, "\n***SHUTDOWN***\n");
  108.         fclose(time_file);
  109.         exit(0);
  110. }
  111.