From 4b1297db98a764d6421328438a4ef5680999a312 Mon Sep 17 00:00:00 2001 From: djfri <djfri@Frisco.attlocal.net> Date: Sun, 5 Aug 2018 23:31:10 -0700 Subject: [PATCH] Homework 2 networking Frisco --- .../Frisco/FriscoAssignment2Client.java | 9 ++-- .../Frisco/FriscoAssignment2Server.java | 41 +++++++------------ 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java index ceac887fdd..4c8c4527ee 100644 --- a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java @@ -23,7 +23,7 @@ public class FriscoAssignment2Client { boolean openConnection = true; try { while (openConnection) { - System.out.println("The Client is creating socket and it's wait for it...its going to be.... Legendary!"); + System.out.println("The Client is creating a socket and it's going to be.... wait for it.... Legen-dary, Lengendary!"); // We request an IP to connect to ("localhost") and // port number at that IP (2317). This establishes @@ -47,11 +47,12 @@ public class FriscoAssignment2Client { System.out.println("Now we're talking!"); System.out.println("\nThe message the server sent was " + serverMessage ); - //System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); + System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); while (openConnection) { - System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); - Scanner incomingMessage = new Scanner(System.in); + //System.out.println("\nSTOP! Who would cross the Bridge of Death must answer me these questions three, ere the other side he see. "); + System.out.println("Answer the questions!"); + Scanner incomingMessage = new Scanner(System.in); String s = incomingMessage.nextLine(); OutputStream os = socket.getOutputStream(); PrintStream ps = new PrintStream(os); diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java index 5f3024e1e8..51c38d105f 100644 --- a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java @@ -19,9 +19,9 @@ import java.net.*; */ public class FriscoAssignment2Server { - public static void main(String[] args) throws IOException { + public static void main(String[] args) { - String[] serverQuestions = new String[7]; //canned responses for multiple inputs from client + String[] serverQuestions = new String[7]; serverQuestions[0] = "What... is your name?"; serverQuestions[1] = "What.... is your quest?"; serverQuestions[2] = "What..... is your favorite color?"; @@ -36,17 +36,16 @@ public class FriscoAssignment2Server { ServerSocket serverSocket = new ServerSocket(2317); System.out.println("Server Socket open and waiting"); - boolean connectionActive; + boolean Active; + - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. while(true) { - Socket clientConnection = serverSocket.accept(); // block until connected - connectionActive = true; //ensure after every new connection the boolean is reset to true. - index =0; //reset the index for responses back to 0 each time a new connection happens. + Socket clientConnection = serverSocket.accept(); + Active = true; + index =0; - while (connectionActive) { + while (Active) { OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); @@ -55,11 +54,7 @@ public class FriscoAssignment2Server { InputStream serverIS = clientConnection.getInputStream(); InputStreamReader serverISR = new InputStreamReader(serverIS); BufferedReader br = new BufferedReader(serverISR); - String line; - - // Print some information locally about the Socket - // connection. This includes the port and IP numbers - // on both sides (the socket pair.) + InetAddress localAddress = clientConnection.getLocalAddress(); InetAddress remoteAddress = clientConnection.getInetAddress(); @@ -68,23 +63,15 @@ public class FriscoAssignment2Server { System.out.println("\nSocket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))"); while (index < 7) { - //while ((line = br.readLine()) != null)//&& index<10) { - - - System.out.println("\n" + br.readLine() ); //line); //prints out what is recieved from client - ps.println("\tThe SERVER has responded with: " + serverQuestions[index]); //sends the prewritten response - System.out.println("Server has sent: " + serverQuestions[index]); //tells server user what was sent - index++; //goes to next response. - - // Notice the use of flush() and close(). Without - // the close() to Socket object may stay open for - // a while after the client has stopped needing this - // connection. Close() explicitly ends the connection. + System.out.println("\n" + br.readLine() ); + ps.println("SERVER: " + serverQuestions[index]); + System.out.println("Server has sent: " + serverQuestions[index]); + index++; } } - connectionActive = false; + Active = false; ps.flush(); clientConnection.close(); } -- GitLab