I have provided the server program at the end....... REPLACE YYY WITH CODE STATE
ID: 3642226 • Letter: I
Question
I have provided the server program at the end.......REPLACE YYY WITH CODE STATEMENTS.....
This is an incomplete version of a client of the question/answer
server qaserver.java.
Complete the program. Comments have been included to
indicate the required tasks. Note that I have used YYY's to
mark places where you must fill in code (not necessarily one
statement).
PROGRAM>
//
// This program asks the user for a question (abbreviated)
// to be asked of the server. It then displays the answer
// sent from the server. Five questions are currently
// supported. "Quit" ends the interaction.
//
// Note that the server is assumed to be running on
// afsconnect1.njit.edu; its port number is given on the
// command-line.
//
// USAGE: java qac <PORT>
//
// where <PORT> is the server's port #.
//
import java.util.*;
import java.net.*;
import java.io.*;
public class qac
{
public static final String USAGE = "USAGE: java qac <PORT>";
public static void main(String[] args)
throws java.io.IOException
{
Socket socket;
boolean finished; // controls loop.
String user_question; // question from user.
String answer; // answer obtained from server.
int port; // from command-line.
String hostname = "afsconnect1.njit.edu";
Scanner sc = new Scanner(System.in);
try
{
// use the port indicated on command-line
port = Integer.parseInt(args[0]);
// create a new socket to connect to server, and
// establish write and read capabilities
socket = new Socket(hostname, port);
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);
Scanner in = new Scanner(new
InputStreamReader(socket.getInputStream()));
// keep getting questions until the user chooses to quit
finished = false;
while (!finished)
{
// get question from the user
YYY
// if input is "Quit", we're finished. Make certain
// to let the server know.
//
// Otherwise, send the question and get a response
if ( YYY )
{
YYY
}
else
{
// send question to server
YYY
// read response and display it
YYY
}
}
out.close();
in.close();
sc.close();
socket.close();
}
catch (IOException e) // socket problems
{
System.out.println(e);
}
catch(NumberFormatException e) // port not a number (int)
{
System.out.println("First argument must be the port number.");
System.out.println(USAGE);
}
catch(ArrayIndexOutOfBoundsException e) // no port # given
{
System.out.println("Need to supply the port number.");
System.out.println(USAGE);
}
} // end main
} // end qac
qaserver.java...
//
// A server that provides answers to some questions sent
// from a client (in abbreviated form). At the moment,
// only five different questions are supported.
//
// Note that the desired port number is supplied as the
// first argument on the command-line.
//
// USAGE: java qaserver <PORT>
//
// where <PORT> is the desired port #.
//
//
//
import java.util.*;
import java.net.*;
import java.io.*;
public class qaserver
{
public static void main(String[] args)
throws java.io.IOException
{
// "database" of the questions (in abbreviated form)
String[] questions = {"Color", // Favorite color
"Pet", // Name of first pet
"Birthday", // Date of birth
"Maiden name", // Mother's maiden name
"Best friend"}; // Best friend's name
// "database" of answers to corresponding questions
String[] answers = {"Blue",
"Spot",
"January 1, 1991",
"Smith",
"Sally"};
ServerSocket serverSocket;
Socket socket;
String inputLine;
String outputLine;
boolean finished;
int index;
int port;
try
{
// use the port indicated on command-line
port = Integer.parseInt(args[0]);
// create a server socket
serverSocket = new ServerSocket(port);
// Listen for a connection request from a client
socket = serverSocket.accept();
// Establish the input and output streams on the socket
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);
Scanner in = new Scanner(new
InputStreamReader(socket.getInputStream()));
// Keep answering questions until the client wants to quit
finished = false;
while(!finished)
{
// get a string from the client
inputLine = in.nextLine();
// If it's "quit," we're done
if(inputLine.equalsIgnoreCase("quit"))
finished = true;
else
{
// Produce the appropriate response to the
// client's question
index = find_question(inputLine, questions);
if(index == -1)
outputLine = "Error: unknown question";
else
outputLine = answers[index];
// send the response to the client
out.println(outputLine);
}
}
out.close();
in.close();
socket.close();
serverSocket.close();
}
catch(IOException e) // socket problems
{
System.out.println(e);
}
catch(NumberFormatException e) // port not a number (int)
{
System.out.println("First argument must be the port number.");
System.out.println("USAGE: java qaserver <PORT>");
}
catch(ArrayIndexOutOfBoundsException e) // no port # given
{
System.out.println("Need to supply the port number.");
System.out.println("USAGE: java qaserver <PORT>");
}
} // end main
/////////////////////////////////////////////////////////////////
public static int find_question(String q, String[] the_qs)
{
// Find the index of the given question q in the "database"
// (array) of questions the_qs. If it does not exist,
// return -1
int retVal;
boolean found;
retVal = 0;
found = false;
while(!found && retVal < the_qs.length)
if(the_qs[retVal].equalsIgnoreCase(q))
found = true;
else
retVal++;
if(!found)
retVal = -1;
return retVal;
} // end find_question
/////////////////////////////////////////////////////////////////
} // end qaserver