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

  1. /*
  2.  * net_client.c
  3.  *
  4.  *    Basic client process for time accounting system, network version.
  5.  *    This process (and those that are linked to it) are run as commands 
  6.  *    and send events to the server process.
  7. */
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #include <stdio.h>
  14. #include "time_entry.h"
  15.  
  16. main(argc, argv)
  17.     int argc;
  18.     char *argv[];
  19. {
  20.  
  21.     int skt_id,    /* Socket descriptor */
  22.         msg_sock,  /* Socket that will accept messages */
  23.         i,j;
  24.  
  25.     EVENT_TYPE event;
  26.  
  27.     struct sockaddr_in sa;  /* Socket address structure   */
  28.     struct hostent *hp,     /* Host entry pointer         */
  29.                    *gethostbyname();   /* Function to get hostinfo by name */
  30.     char buff[80];
  31.  
  32.         /*
  33.          *  Specify the socket domain and type we want...
  34.         */
  35.         skt_id = socket(AF_INET, SOCK_STREAM, 0);
  36.         if(skt_id == -1){
  37.             perror("Can't create socket");
  38.             exit(1);
  39.         }
  40.  
  41.         /*
  42.          *  Now set up the address structure and connect to the socket.
  43.         */
  44.         sa.sin_family = AF_INET;
  45.         hp = gethostbyname("utopia");  /* Running server on machine utopia */
  46.         if(!hp){
  47.             perror("Can't find utopia");
  48.             exit(1);
  49.         }
  50.  
  51.         /*
  52.          * Copy the host address from the pointer retruned in the 
  53.          * gethostbyname() call into the address member of the
  54.          * socket address structure.
  55.         */
  56.  
  57.         bcopy(hp->h_addr, &sa.sin_addr, hp->h_length);
  58.  
  59.         /*
  60.          *  For this example, the port number retruned when the server
  61.          *  is started is enterd on the command line for the client.
  62.          *  In reality, the port would be assigned or made available
  63.          *  in some other manner.
  64.         */
  65.  
  66.         sa.sin_port = htons(atoi(argv[1]));
  67.  
  68.         if(connect(skt_id, &sa, sizeof(sa)) == -1){
  69.             perror("Can't connect to socket");
  70.             exit(1);
  71.         }
  72.  
  73.         build_event(argv[0], &event);
  74.  
  75.         /* 
  76.          * Now, send the event
  77.         */
  78.  
  79.         if(write(skt_id, &event, sizeof(event)) != sizeof(event)){
  80.             perror("Write");
  81.             exit(1);
  82.         }
  83.  
  84.         /* Cleanup */
  85.         close(skt_id);
  86.         exit(0);
  87. }
  88.