Home Segments Index Top Previous Next

733: Mainline

At this point, a call to fscanf can read in data that have been supplied by the client, and a call to fprintf can write out processed data that is to be supplied to the client.

Suppose, for example, that you want to set up a server program that computes the number of years it takes to double money at an interest rate supplied by a client program.

Presuming that the function on the server that actually computes the doubling time is the doubling_time function shown in Segment 117, the following while loop does the job:

while (1) { 
  double input; 
  fscanf (fm_client, "%lf", &input); 
  fprintf (to_client, "%f\n", doubling_time(input)); 
  fflush (to_client); 
} 

Note that the input variable is declared inside the while loop; this declaration is legitimate because any block can introduce local variables. Note also the call to fflush; this call is needed to ensure that the information supplied in the call to fprintf is passed along, and is not left stranded in a buffer.