Home Segments Index Top Previous Next

742: Mainline

Note that the server and client programs suffer from a serious defect: Inasmuch as there is much that can go wrong when servers and clients try to get together, defensive-programming code should be added to provide progress information. Accordingly, the following server and client programs are easier to work with in practice, although they are more complex and difficult to read:

/* Server Program */ 
#include  
#include  
#include  
#include  
#include  
#include  
double doubling_time (double r) {return 72.0 / r;} 
main ( ) { 
  int socket_number, new_socket_number, client_size; 
  struct sockaddr_in server_addr, client_addr; 
  FILE /fm_client, /to_client; 
  /* Initialize structures */ 
  bzero(&server_addr, sizeof(struct sockaddr_in));       
  bzero(&client_addr, sizeof(struct sockaddr_in)); 
  server_addr.sin_addr.s_addr = inet_addr ("128.52.36.4"); 
  server_addr.sin_family = AF_INET; 
  server_addr.sin_port = 6022; 
  if ((socket_number = socket (AF_INET, SOCK_STREAM, 0)) < 0) { 
    fprintf (stderr, "Server could not obtain a socket !!!\n");     
    exit (1); 
  } 
  if (bind (socket_number, 
            (struct sockaddr /)&server_addr, 
            sizeof (server_addr)) 
       < 0) { 
    fprintf (stderr, "Server could not bind name !!!\n"); 
    exit (1); 
  } 
  listen (socket_number, 5); 
  if ((new_socket_number 
        = accept (socket_number, 
                  (struct sockaddr /) &client_addr, 
                  &client_size)) 
      < 0) { 
    fprintf (stderr, "Server could not accept connection !!!\n"); 
    exit (1); 
  } 
  fm_client = (FILE /) fdopen (new_socket_number, "r"); 
  if (NULL == fm_client) { 
    fprintf (stderr, "Server could not open read stream !!!\n"); 
    exit (1); 
  } 
  to_client = (FILE /) fdopen (new_socket_number, "w"); 
  if (NULL == to_client) { 
    fprintf (stderr, "Server could not open write stream !!!\n"); 
    exit (1); 
  } 
  while (1) { 
    double input; 
    if (EOF == fscanf (fm_client, "%lf", &input)) { 
      fprintf (stderr, "Read from client failed !!!\n"); exit (1); 
    } 
    if (fprintf (to_client, "%f\n", doubling_time (input)) < 0) { 
      fprintf (stderr, "Write to client failed !!!\n"); exit (1); 
    } 
    fflush (to_client); 
  } 
} 

/* Client Program */ 
#include  
#include  
#include  
#include  
#include  
main ( ) {  
  int socket_number; 
  struct sockaddr_in server_addr; 
  FILE /fm_server, /to_server; 
  server_addr.sin_family = AF_INET; 
  server_addr.sin_port = 6022; 
  server_addr.sin_addr.s_addr = inet_addr ("128.52.36.4"); 
  socket_number = socket (AF_INET, SOCK_STREAM, 0); 
  if ((socket_number = socket (AF_INET, SOCK_STREAM, 0)) < 0) { 
    fprintf (stderr, "Client could not obtain a socket !!!\n");     
    exit (1); 
  } 
  if (connect (socket_number,  
               (struct sockaddr /)&server_addr, 
               sizeof (server_addr)) < 0) { 
    fprintf (stderr, "Client could not connect !!!\n"); exit (1); 
  } 
  to_server = fdopen (socket_number, "w"); 
  if (NULL == to_server) { 
    fprintf (stderr, "Client could not open write stream !!!\n"); 
    exit (1); 
  } 
  fm_server = fdopen (socket_number, "r"); 
  if (NULL == fm_server) { 
    fprintf (stderr, "Client could not open read stream !!!\n"); 
    exit (1); 
  } 
  while (1) { 
    double keyed_number, received_number; 
    printf ("Please type an interest rate:\n> "); 
    scanf ("%lf", &keyed_number); 
    if (fprintf (to_server, "%f\n", keyed_number) < 0) { 
      fprintf (stderr, "Write to server failed !!!\n"); exit (1); 
    } 
    fflush (to_server); 
    if (EOF == fscanf (fm_server, "%lf", &received_number)) { 
       fprintf (stderr, "Read from server failed !!!\n"); exit (1); 
    } 
    printf ("Server returned doubling time of %.1f years\n", 
            received_number); 
  } 
}