home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / src / cellsock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-26  |  2.9 KB  |  122 lines

  1. /*****
  2.  *
  3.  * File: cellsock.c
  4.  *
  5.  * Cellsim, cellular automata simulator
  6.  *
  7.  * Handles socket functions for doing analysis of state-counts
  8.  *
  9.  *****/
  10.  
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. #include <stdio.h>
  16.  
  17. #include "cell.def"
  18.  
  19. /*
  20.  *
  21.  * Cellsim copyright 1989, 1990 by Chris Langton and Dave Hiebeler
  22.  * (cgl@lanl.gov, hiebeler@heretic.lanl.gov)
  23.  *
  24.  * This package may be freely distributed, as long as you don't:
  25.  * - remove this notice
  26.  * - try to make money by doing so
  27.  * - prevent others from copying it freely
  28.  * - distribute modified versions without clearly documenting your changes
  29.  *   and notifying us
  30.  *
  31.  * Please contact either of the authors listed above if you have questions
  32.  * or feel an exception to any of the above restrictions is in order.
  33.  *
  34.  * If you make changes to the code, or have suggestions for changes,
  35.  * let us know!  If we use your suggestion, you will receive full credit
  36.  * of course.
  37.  */
  38.  
  39. /*****
  40.  * Cellsim history:
  41.  *
  42.  * Cellsim was originally written on Apollo workstations by Chris Langton.
  43.  *
  44.  * Sun versions:
  45.  *
  46.  * - version 1.0
  47.  *   by C. Ferenbaugh and C. Langton
  48.  *   released 09/02/88
  49.  *
  50.  * - version 1.5
  51.  *   by Dave Hiebeler and C. Langton  May - June 1989
  52.  *   released 07/03/89
  53.  *
  54.  * - version 2.0
  55.  *   by Dave Hiebeler and C. Langton  July - August 1989
  56.  *   never officially released (unofficially released 09/08/89)
  57.  *
  58.  * - version 2.5
  59.  *   by Dave Hiebeler and C. Langton  September '89 - February 1990
  60.  *   released 02/26/90
  61.  *****/
  62.  
  63.  
  64. static int sock;
  65. extern int *statecount;
  66. extern short S;
  67.  
  68. init_sock(hname, portnum)        /* create socket and connect to given port */
  69.     char   *hname;            /* host name ("" = this machine) */
  70.     int     portnum;            /* port number */
  71. {
  72.     struct sockaddr_in server;
  73.     struct hostent *hp, *gethostbyname();
  74.     char    hostname[BUFLEN];
  75.     int     i, num;
  76.  
  77.     /* Create socket */
  78.     sock = socket(AF_INET, SOCK_STREAM, 0);
  79.     if (sock < 0) {
  80.     perror("opening stream socket");
  81.     exit(1);
  82.     }
  83.     /* Find hostname, if necessary */
  84.     if (hname[0] == 0)
  85.     gethostname(hostname, BUFLEN);
  86.     else
  87.     strncpy(hostname, hname, BUFLEN);
  88.  
  89.     /* Connect socket using name specified by command line. */
  90.     server.sin_family = AF_INET;
  91.     hp = gethostbyname(hostname);
  92.     if (hp == 0) {
  93.     fprintf(stderr, "%s: unknown host\n", hostname);
  94.     exit(1);
  95.     }
  96.     bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
  97.     server.sin_port = htons(portnum);
  98.  
  99.     if (connect(sock, &server, sizeof(server)) < 0) {
  100.     perror("connecting stream socket");
  101.     exit(1);
  102.     }
  103.     num = S;
  104.     if (write(sock, (char *) &num, sizeof(num)) < 0)
  105.     perror("writing on stream socket");
  106. } /* init_sock */
  107.  
  108.  
  109. send_sock()
  110. {                    /* send current statecounts across
  111.                      * socket */
  112.     int     i;
  113.     if (write(sock, (char *) statecount, S * sizeof(*statecount)) < 0)
  114.     perror("writing on stream socket");
  115. }
  116.  
  117.  
  118. quit_sock()
  119. {                    /* close socket */
  120.     close(sock);
  121. }
  122.