This is an incomplete version of a client of the question/answer server qaserver
ID: 3659189 • Letter: T
Question
This is an incomplete version of a client of the question/answer
server qaserver.java
It has minor mistakes of not communicating PLEASE FIX..
PROGRAM>
import java.util.*;
import java.net.*;
import java.io.*;
/**
* The Class QAC.
*/
public class QAC {
/** The Constant USAGE. */
public static final String USAGE = "USAGE: java qac <PORT>";
/**
* The main method.
*
* @param args the arguments
* @throws IOException Signals that an I/O exception has occurred.
*/
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
System.out.println("Enter the question: ");
user_question = sc.nextLine();
// if input is "Quit", we're finished. Make certain
// to let the server know.
// Otherwise, send the question and get a response
if (user_question.equalsIgnoreCase("quit")) {
finished = true;
} else {
// send question to server
out.write(user_question.toCharArray());
// read response and display it
answer = in.nextLine();
System.out.println("Answer from server: "+answer);
}
}
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
Explanation / Answer
package org.ilzd.netexamples; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * Simple server that listens for a question and provides an answer. * What's missing: error handling, and a well-defined protocol. * * @author jlepak */ public class QAServer { public static final int PORT = 8888; public static void main(String[] args) throws IOException { // Set up server. localhost is the host name of the loopback // interface, which you use to communicate with other processes // only on your own computer. String addr = "10.38.159.59"; ServerSocket server = new ServerSocket(PORT, 0, InetAddress.getByName(addr)); System.out.format("QAServer running on %s:%s ", addr, PORT); // Read input line from client. while (true) { Socket client = null; client = server.accept(); InputStream clientInput = client.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(clientInput)); String line = reader.readLine(); System.out.println(line); // Send output line to client. OutputStream clientOutput = client.getOutputStream(); // Second argument indicates that all println() calls // should flush output. PrintWriter writer = new PrintWriter(clientOutput, true); Scanner answerScanner = new Scanner(System.in); writer.println(answerScanner.nextLine()); reader.close(); writer.close(); client.close(); } // server.close(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.