From 067110d1f895b435ca0035a5de35905a8f15eb76 Mon Sep 17 00:00:00 2001 From: djfri <djfri@Frisco.attlocal.net> Date: Sun, 5 Aug 2018 21:32:39 -0700 Subject: [PATCH] Homework 2 networking Frisco --- .../Frisco/FriscoAssignment2Client.java | 80 +++++++++++++++ .../Frisco/FriscoAssignment2Server.java | 99 +++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java create mode 100644 deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java new file mode 100644 index 0000000000..ceac887fdd --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java @@ -0,0 +1,80 @@ +package MV3500Cohort2018JulySeptember.homework2.Frisco; + +import java.io.*; +import java.net.*; +import java.util.Scanner; + +/** + * Before, we always used telnet to connect to the server. Here we are now + * writing our own program to do the connection. + * + * As you will see, when we run this after we start the server we will see the + * same string telnet printed, sent by the server. The output at the server will + * show different socket pairs for each time we ran it. + * + * @author mcgredo + */ + +public class FriscoAssignment2Client { + + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 + + public static void main(String[] args) { + 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!"); + + // 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. + Socket socket = new Socket(LOCALHOST, 2317); // locohost? who be crazy now? + + // Now hook everything up (i.e. set up the streams), Java style: + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + // Read the single line written by the server. We'd + // do things a bit differently if many lines to be read + // from the server, instead of one only. + + // + String serverMessage = br.readLine(); + System.out.println("=================================================="); + 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. "); + 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); + String s = incomingMessage.nextLine(); + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); + ps.println("Client Reply: " + s); + + serverMessage = br.readLine(); + System.out.println(serverMessage); + if(serverMessage.contains("Huh")) + { + openConnection = false; + socket.close(); + } + // aboved lines of code were ideas of Maj Furr on how to get the client to type to the server, + //this is what I was also doing so I borrowed his code + + } + } + } catch (IOException e) { + System.out.println("Problem with client: "); // describe what is happening + System.out.println(e); + } + // program exit: tell somebody about that + System.out.println("client exit.. YOU may Pass!!"); + } + +} diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java new file mode 100644 index 0000000000..5f3024e1e8 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Server.java @@ -0,0 +1,99 @@ +package MV3500Cohort2018JulySeptember.homework2.Frisco; + +import java.io.*; +import java.net.*; + +/** + * Very slightly more complex than example1. A complete copy of example 2. + * The only thing this does differently is introduce a loop into the response, so + * you don't have to restart the program after one response. Also, it prints out + * the socket pair the server sees. Run the program via telnet several times and + * compare the socket pairs.telnet localhost 2317 + * If you're sophisticated you can contact the instructor's computer while running this program. + * + * telnet <ipOfServersLaptop> 2317 + * + * And have him display the socket pairs he got. + * + * @author mcgredo + */ +public class FriscoAssignment2Server { + + public static void main(String[] args) throws IOException { + + String[] serverQuestions = new String[7]; //canned responses for multiple inputs from client + serverQuestions[0] = "What... is your name?"; + serverQuestions[1] = "What.... is your quest?"; + serverQuestions[2] = "What..... is your favorite color?"; + serverQuestions[3] = "What... is your name?"; + serverQuestions[4] = "What.... is your quest?"; + serverQuestions[5] = "What..... is the air-speed velocity of an unladen swallow?"; + serverQuestions[6] = "Huh ? I--I don't know that. Auuuuuuuugh!!"; + int index = 0; + + try { + + + ServerSocket serverSocket = new ServerSocket(2317); + System.out.println("Server Socket open and waiting"); + boolean connectionActive; + + // 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. + + while (connectionActive) { + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + ps.println("This was written by the server"); + + 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(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); + 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. + } + + } + connectionActive = false; + ps.flush(); + clientConnection.close(); + } + } + + } catch (IOException e) { + System.out.println("Danger Danger Will Robinson!! the Network is failing" + e); + } + + } + +} -- GitLab