Your task is to connect to the oracle server at port 7777 on the local host. The oracle protocol is very simple. Send a single line containing a question to the oracle server, and the oracle server will send the answer in a single line and then disconnect.

Complete the following file:

OracleClient.java

import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class OracleClient { public static String getAnswer(String question) throws IOException { OracleServer.start(); final int PORT = 7777; // Connect to the port on localhost // Send the question // Get the response // Close the socket // Return the response } }

Use the following files:

OracleServer.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
   A server that receives client connections to the oracle service.
*/
public class OracleServer
{  
   public static void main(String[] args) throws IOException
   {  
      final int PORT = 7777;
      ServerSocket server = new ServerSocket(PORT);
      System.out.println("Waiting for clients to connect...");
      
      while (true)
      {
         Socket s = server.accept();
         System.out.println("Client connected.");
         OracleService service = new OracleService(s);
         Thread t = new Thread(service);
         t.start();
      }
   }
   
   public static void start()
   {
      class ServerRunnable implements Runnable
      {
         public void run() 
         { 
            try 
            { 
               OracleServer.main(null); 
            } 
            catch (IOException ex) 
            {}
         }
      }
      
      Runnable r = new ServerRunnable();
      Thread t = new Thread(r);
      t.setDaemon(true); // this thread will die if it is the only one left
      t.start(); // start up server in new thread
   }
}

OracleService.java

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
   Answers a question that was sent from a socket.
*/
public class OracleService implements Runnable
{
   /**
      Constructs a service object that processes a question.
      @param aSocket the socket
   */
   public OracleService(Socket aSocket)
   {
      s = aSocket;
   }

   public void run()
   {
      try
      {
         try
         {
            in = new Scanner(s.getInputStream());
            out = new PrintWriter(s.getOutputStream());
            String command = in.nextLine();
            
            String[] questionWords = { "where", "when", "what", "why", "how",
               "who", "which" };
            boolean isQuestion = false;
            for (int i = 0; !isQuestion && i < questionWords.length; i++)         
               if (command.toLowerCase().startsWith(questionWords[i] + " "))
                  isQuestion = true;
            if (isQuestion)
               out.println("The answer is 42.");
            else
               out.println("That was not a question.");
            out.flush();
         }
         finally
         {
            s.close();
         }
      }
      catch (IOException exception)
      {
         exception.printStackTrace();
      }
   }

   private Socket s;
   private Scanner in;
   private PrintWriter out;
}