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

  1. /****************************************\
  2. *                                        *
  3. *           dataread.c                *
  4. *                         *
  5. *        read data from a socket         *
  6. *                                        *
  7. *      for use with CA simulator        *
  8. *                                        *
  9. *                                        *
  10. \****************************************/
  11.  
  12. /* Aug 30 1988 */
  13.  
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <netdb.h>
  18. #include <stdio.h>
  19. #define TRUE 1
  20. #define MAXLEN 32
  21.  
  22. /*
  23.  * Format of socket messages from CA simulator is as follows:  The first message
  24.  * is a single integer, giving the number of states per cell currently in use.
  25.  * Subsequent messages are sent once every CA time step; each message is a list
  26.  * of integers, one per state, giving a count of the number of cells in the current
  27.  * array with that state.
  28.  */
  29.  
  30. /*
  31.  * This program creates a socket and then begins an infinite loop.  Each time
  32.  * through the loop it accepts a connection and prints out messages from it.
  33.  * The first message is assumed to be a single integer, giving the length (in
  34.  * integers) of all subsequent messages.  When the connection breaks, or a
  35.  * termination message comes through, the program accepts a new connection.
  36.  */
  37.  
  38. int     sock, msgsock;
  39.  
  40. main()
  41. {
  42.     int     buf[MAXLEN], rval, msglen;
  43.     int     i;
  44.  
  45.     setup_receiver();
  46.  
  47.     do {
  48.     connect_receiver();
  49.     rval = get_message(&msglen, 1);
  50.     while (rval != 0) {
  51.         bzero(buf, sizeof(buf));
  52.         rval = get_message(buf, msglen);    /* get list of integers */
  53.         if (rval == 0)
  54.         break;
  55.         printf("[");        /* print out the list */
  56.         for (i = 0; i < msglen; i++)
  57.         printf(" %d", buf[i]);
  58.         printf("]\n");
  59.         fflush(stdout);
  60.     }
  61.     disconnect_receiver();
  62.     } while (TRUE);
  63.  
  64.     /*
  65.      * Since this program has an infinite loop, the socket "sock" is never
  66.      * explicitly closed.  However, all sockets will be closed automatically
  67.      * when a process is killed or terminates normally.
  68.      */
  69.  
  70.     close_receiver();
  71. }
  72.  
  73. setup_receiver()
  74. {
  75.     struct sockaddr_in server;
  76.     int     length;
  77.  
  78.     /* Create socket */
  79.     sock = socket(AF_INET, SOCK_STREAM, 0);
  80.     if (sock < 0) {
  81.     perror("opening stream socket");
  82.     exit(1);
  83.     }
  84.     /* Name socket using wildcards */
  85.     server.sin_family = AF_INET;
  86.     server.sin_addr.s_addr = INADDR_ANY;
  87.     server.sin_port = 0;
  88.     if (bind(sock, &server, sizeof(server))) {
  89.     perror("binding stream socket");
  90.     exit(1);
  91.     }
  92.     /* Find out assigned port number and print it out */
  93.     length = sizeof(server);
  94.     if (getsockname(sock, &server, &length)) {
  95.     perror("getting socket name");
  96.     exit(1);
  97.     }
  98.     fprintf(stderr, "Socket has port #%d\n", ntohs(server.sin_port));
  99.  
  100.     /* Start accepting connections */
  101.     listen(sock, 5);
  102. }
  103.  
  104. connect_receiver()
  105. {
  106.     msgsock = accept(sock, 0, 0);
  107.     if (msgsock == -1) {
  108.     perror("accept");
  109.     exit(1);
  110.     }
  111. }
  112.  
  113. disconnect_receiver()
  114. {
  115.     close(msgsock);
  116. }
  117.  
  118. get_message(data, len)            /* Receives len integers into array
  119.                      * data. Returns 0 if connection
  120.                      * closed.      */
  121.     int    *data, len;
  122. {
  123.     int     r;
  124.     if ((r = read(msgsock, (char *) data, len * sizeof(int))) < 0)
  125.     perror("reading stream message");
  126.     if (r == 0)
  127.     fprintf(stderr, "Ending connection\n");
  128.     return (r);
  129. }
  130.  
  131. close_receiver()
  132. {
  133.     close(sock);
  134. }
  135.  
  136.  
  137. /***** END OF FILE *****/
  138.