package chapter18;

/**
 * Title:        Chapter 18, "Networking"
 * Description:  Examples for Chapter 18
 * Copyright:    Copyright (c) 2000
 * Company:      Armstrong Atlantic State University
 * @author Y. Daniel Liang
 * @version 1.0
 */

// Client.java: The client sends the input to the server and receives
// result back from the server
import java.io.*;
import java.net.*;
import chapter2.MyInput;

public class Client
{
  /**Main method*/
  public static void main(String[] args)
  {
    try
    {
      // Create a socket to connect to the server
      // Socket connectToServer = new Socket("130.254.32.8", 8000);
      // Socket connectToServer = new Socket("drake.armstrong.edu", 8000);
      Socket connectToServer = new Socket("localhost", 8000);

      // Create an input stream to receive data from the server
      DataInputStream isFromServer = new DataInputStream(
        connectToServer.getInputStream());

      // Create an output stream to send data to the server
      DataOutputStream osToServer =
        new DataOutputStream(connectToServer.getOutputStream());

      // Continuously send radius and receive area from the server
      while (true)
      {
        // Read the radius from the keyboard
        System.out.print("Please enter a radius: ");
        double radius = MyInput.readDouble();

        // Send the radius to the server
        osToServer.writeDouble(radius);
        osToServer.flush();

        // Get area from the server
        double area = isFromServer.readDouble();

        // Print area on the console
        System.out.println("Area received from the server is "
          + area);
      }
    }
    catch (IOException ex)
    {
      System.err.println(ex);
    }
  }
}