Your task is to provide an oracle server at port 7777 on the local host. The oracle protocol is very simple. The client sends a single line containing a question to the oracle server, and the oracle server sends the answer in a single line and then disconnects.

The oracle server is not a real oracle. It simply checks whether the input is an actual question (that is, a line starting with "where", "when", "what", "why", "how", "who", "which"). If so, it sends the response "The answer is 42.". Otherwise, it sends the response "That was not a question."

Complete the following file:

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 { // Get scanner and print writer // get input line // check if it is a question // send answer out.flush(); } finally { s.close(); } } catch (IOException exception) { exception.printStackTrace(); } } private Socket s; private Scanner in; private PrintWriter out; }

Use the following files:

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;
      Socket s = new Socket("localhost", PORT);
      InputStream instream = s.getInputStream();
      OutputStream outstream = s.getOutputStream();
      Scanner in = new Scanner(instream);
      PrintWriter out = new PrintWriter(outstream); 
      
      out.println(question);
      out.flush();
      String response = in.nextLine();
      s.close();
      return response;      
   }
}

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
   }
}