package TcpExamples; import static TcpExamples.TcpSentryScenarioHandlerThread.HALT_WHO_GOES_THERE; import static TcpExamples.TcpSentryScenarioHandlerThread.HOLD_IT_RIGHT_THERE; import static TcpExamples.TcpSentryScenarioHandlerThread.YOU_MAY_PASS; import java.io.*; import java.net.*; /** * This client program establishes a socket connection to the {@link TcpSentryScenarioDispatchServer}, * then checks how long it takes to read the single line it expects as a server response. * No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}. * * @see TcpSentryScenarioDispatchServer * @see TcpSentryScenarioHandlerThread * @see TcpExample4Client * @see TcpExample4DispatchServer * @see TcpExample4HandlerThread * * @see <a href="../../../src/TcpExamples/TcpSentryScenarioTerminalLog.txt" target="blank">TcpSentryScenarioTerminalLog.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> * * @author Don Brutzman * @author Don McGregor * @author MV3500 class */ public class TcpSentryScenarioClient { static String prefix = "[" + TcpSentryScenarioClient.class.getName() + "] "; /** Default constructor */ public TcpSentryScenarioClient() { // default constructor } static String DESTINATION_HOST = "localhost"; static int MAX_LOOP_COUNT = 6; /** * Program invocation, execution starts here * @param args command-line arguments */ public static void main(String[] args) { try { System.out.println(prefix + " start, loop " + MAX_LOOP_COUNT + " times"); System.out.println("======================================================="); for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit { System.out.println(prefix + " creating new socket #" + loopCount + "..."); // We request an IP to connect to ("localhost") and // port number at that IP (2317). This establishes // a connection to that IP in the form of the Socket // object; the server uses a ServerSocket to wait for // connections. // open a socket for each loop Socket socket = new Socket(DESTINATION_HOST, 2317); //////////////////////////////////////////////////////////////////////////////////////////// // Assignment code String myName = new String(); // input stream and output stream InputStream inputStream = socket.getInputStream(); Reader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); OutputStream outputStream = socket.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); String serverMessage = bufferedReader.readLine(); // blocks System.out.println(prefix + ": message received from server='" + serverMessage + "'"); String sentryResponse = bufferedReader.readLine(); System.out.println ("Sentry: " + sentryResponse); if (sentryResponse.equals(HALT_WHO_GOES_THERE)) { // user provides name from console myName = System.console().readLine(); printStream.println(myName); // send it back to dispatch server // System.out.println ("[trace] console return: " + myName); sentryResponse = bufferedReader.readLine(); System.out.println ("Sentry: " + sentryResponse); if (sentryResponse.equals(YOU_MAY_PASS)) { System.out.println ("Thank you officer."); } else if (sentryResponse.equals(HOLD_IT_RIGHT_THERE)) { System.out.println ("OK I am outta here!"); } // handling unexpected cases is important else System.out.println ("I'm not sure what that means..."); } System.out.println("======================================================="); // To push this further, launch multiple copies of TcpSentryScenarioClient simultaneously if (myName.equalsIgnoreCase("quit") || myName.equalsIgnoreCase("exit")) { printStream.flush(); socket.close(); } } //////////////////////////////////////////////////////////////////////////////////////////// System.out.println(prefix + " complete"); // main method now exits } catch (IOException e) { System.out.println("Problem with " + prefix + " networking: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!"); } } } }