package MV3500Cohort2024JulySeptember.homework2.Matiski; import java.io.*; import java.net.*; /** * This client program establishes a socket connection to the {@link TcpExamples.TcpExample4DispatchServer}, * then checks how long it takes to read the single line it expects as a server response. * The modification I have added is it checks for a password from the server. And it matches it says success and disconnects * No fancy footwork here, it is pretty simple and similar to {@link TcpExamples.TcpExample3Client}. * * @see TcpExample4DispatchServer * @see TcpExample4HandlerThread * * @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a> * @see <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a> * @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a> * credit to the following for providing the class example that I modified * @author Don McGregor * @author Don Brutzman * @author MV3500 class * @MarkMM */ public class Matiski2Client { /** Default constructor */ public Matiski2Client() { // default constructor } static String DESTINATION_HOST = "localhost"; static int MAX_LOOP_COUNT = 4; /** * Program invocation, execution starts here * @param args command-line arguments */ public static void main(String[] args) { try { System.out.println(Matiski2Client.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times"); System.out.println("======================================================="); for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit { System.out.println(Matiski2Client.class.getName() + " creating new socket #" + loopCount + "..."); long startTime = System.currentTimeMillis(); // Open a socket for each loop Socket socket = new Socket(DESTINATION_HOST, 2317); // Note our local port on this client host is not specified - because // that doesn't matter. It is an "ephemeral" port on localhost. // Setup. Read the prompt from the server. InputStream is = socket.getInputStream(); Reader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); OutputStream os = socket.getOutputStream(); PrintStream ps = new PrintStream(os); // Read server prompt for password String serverPrompt = br.readLine(); System.out.println(serverPrompt); // Should print "Enter password:" // Send the password String password = "Mark"; // Replace with your password ps.println(password); ps.flush(); // Read server response String serverResponse = br.readLine(); System.out.println(serverResponse); if ("Password accepted. Processing your request...".equals(serverResponse)) { // Read the actual message from the server String serverMessage = br.readLine(); long readTime = System.currentTimeMillis(); long timeLength = readTime - startTime; System.out.println(Matiski2Client.class.getName() + ": message received from server='" + serverMessage + "'"); System.out.println(Matiski2Client.class.getName() + ": time msec required for read=" + timeLength); } System.out.println("======================================================="); socket.close(); // Close the socket after the interaction } System.out.println(Matiski2Client.class.getName() + " complete"); // main method now exits } catch (IOException e) { System.out.println("Problem with " + Matiski2Client.class.getName() + " networking:"); // describe what is happening System.out.println("Error: " + e); // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) { System.out.println("*** Be sure to stop any other running instances of programs using this port!"); } else if (e instanceof java.net.ConnectException) { System.out.println("*** Be sure to start the server before starting the client!"); } } } }