diff --git a/projects/Assignments/homework1/BlankenbeckerConsoleOutput.txt b/projects/Assignments/homework1/BlankenbeckerConsoleOutput.txt new file mode 100644 index 0000000000000000000000000000000000000000..c35183aa3565b58c6a1b7eed48a21552b546d80c --- /dev/null +++ b/projects/Assignments/homework1/BlankenbeckerConsoleOutput.txt @@ -0,0 +1,13 @@ +Subject: HW console output +From:"Blankenbeker, Michael (Capt)" <mjblanke@nps.edu> + +Console output for HW shows the following: + +client class: +creating socket +The message the server sent was This was written by the server + +server class: +The message the client sent was This response was written by myTcpClient +Socket pair: (( /127.0.0.1, 2317 ), ( /127.0.0.1, 56950 )) +Connection count is: 1 diff --git a/projects/Assignments/homework1/BlankenbekerMyTcpClient.java b/projects/Assignments/homework1/BlankenbekerMyTcpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..bda38a71cf8ec54d3c578e599bbbbbb21539811d --- /dev/null +++ b/projects/Assignments/homework1/BlankenbekerMyTcpClient.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +//package mv3500code; +import java.io.*; +import java.net.*; + +/** + * Before, we always used telnet to connect to the server. 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 BlankenbekerMyTcpClient { + + + public static void main(String[] args) + { + try + { + while(true){ + + + System.out.println("creating socket"); + + // 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); + + // 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. + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String serverMessage = br.readLine(); + System.out.println("The message the server sent was " + serverMessage); + + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This response was written by myTcpClient"); // to remote client + + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + + } + } + catch(IOException e) + { + System.out.println(e); + System.out.println("Problem with client :" + e); + } + System.out.println("client exit"); + } + +} diff --git a/projects/Assignments/homework1/BlankenbekerMyTcpServer.java b/projects/Assignments/homework1/BlankenbekerMyTcpServer.java new file mode 100644 index 0000000000000000000000000000000000000000..4dc227890b63db663d542795fc12b6a4bf95a6b1 --- /dev/null +++ b/projects/Assignments/homework1/BlankenbekerMyTcpServer.java @@ -0,0 +1,95 @@ +//package mv3500code; + +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 BlankenbekerMyTcpServer +{ + + public static void main(String[] args) + { + try + { + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + int connectionCount = 0; + ServerSocket serverSocket = new ServerSocket(2317); + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + Socket clientConnection = serverSocket.accept(); + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + connectionCount++; + ps.println("This was written by the server"); + + InputStream is = clientConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String serverMessage = br.readLine(); + System.out.println("The message the client sent was " + serverMessage); + + // 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(); + + // My socket pair connection looks like this, to localhost: + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + // + // Why is the first IP/port the same, while the second set has + // different ports? + + + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + System.out.println("Connection count is: " + connectionCount); + + // 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. + ps.flush(); + clientConnection.close(); + } + } + catch(Exception e) + { + System.out.println("problem with networking"); + } + + } + + + +}