Listing 5 (net_client.c)

/*
 * Basic client process for time accounting system, network version.
 * This process (and those that are linked to it) are run as commands
 * and send events to the server process.
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include "time_entry.h"

main(argc, argv)
   int argc;
   char *argv[];
{

   int skt_id,     /* Socket descriptor */
       msg_sock,  /* Socket that will accept messages */
       i,j;

   EVENT_TYPE event;

   struct sockaddr_in sa;  /* Socket address structure */
   struct hostent *hp,     /* Host entry pointer       */
                *gethostbyname();  /* Function to get hostinfo by name */
   char buff[80];

      /*
       * Specify the socket domain and type we want...
      */
      skt_id = socket(AF_INET, SOCK_STREAM, 0);
      if(skt_id == -1){
          perror("Can't create socket");
          exit(1);

      }

      /*
       * Now set up the address structure and connect to the socket.
      */
      sa.sin_family = AF_INET;
      hp = gethostbyname("utopia");  /* Running server on machine utopia */
      if(!hp){
         perror("Can't find utopia");
         exit(1);
      }

      /*
       * Copy the host address from the pointer retruned in the
       * gethostbyname() call into the address member of the
       * socket address structure.
      */

      bcopy(hp->h_addr, &sa.sin_addr, hp->h_length);

      /*
       * For this example, the port number retruned when the server
       * is started is enterd on the command line for the client.
       * In reality, the port would be assigned or made available
       * in some other manner.
      */

      sa.sin_port = htons(atoi(argv[1]));
      
      if(connect(skt_id, &sa, sizeof(sa)) == -1){
         perror("Can't connect to socket");
         exit(1);
      }

      build_event(argv[0], &event);

      /*
       * Now, send the event
      */

      if(write(skt_id, &event, sizeof(event)) != sizeof(event)){
         perror("Write");
         exit(1);
      }

      /* Cleanup */
      close(skt_id);
      exit(0);
}