diff --git a/projects/Assignments/homework1/TackettTcpClient.java b/projects/Assignments/homework1/TackettTcpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..c742f9147f5ec9e9eaea2f8b8eeabd2b11002d93 --- /dev/null +++ b/projects/Assignments/homework1/TackettTcpClient.java @@ -0,0 +1,84 @@ +//package tcpclient; +import java.net.Socket; + +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 TackettTcpClient { + + public final static String LOCALHOST = "localhost"; + //String can also be IPV4 127.0.0.1 or IPV6 0:0:0:0:0:0:0:1 + + 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); + + OutputStream os = socket.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + BufferedWriter bw = new BufferedWriter(osw); + + String locationMessage = "Enemy infantry spotted in the open at position Alpha"; + + String sendMessage = locationMessage + "\n"; + bw.write(sendMessage); + bw.flush(); + System.out.println("Message sent to the server: " + sendMessage); + + // 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 message = br.readLine(); + System.out.println("Message received from the server: "+message); + + String serverMessage = br.readLine(); + System.out.println("Now we're talking"); + System.out.println("The message the server sent was " + serverMessage); + } + catch(Exception exception) + { + // System.out.println(e); + exception.printStackTrace(); + System.out.println("Problem with client"); + System.out.println(exception); + } + finally + { + try + { + //socket.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + System.out.println("client exit"); + } + } + + + + diff --git a/projects/Assignments/homework1/TackettTcpServer.java b/projects/Assignments/homework1/TackettTcpServer.java new file mode 100644 index 0000000000000000000000000000000000000000..993019f1f4db7db612287a95afa5331a1b5317d7 --- /dev/null +++ b/projects/Assignments/homework1/TackettTcpServer.java @@ -0,0 +1,102 @@ +//package tcpserver; +import java.net.Socket; +import java.net.ServerSocket; + +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 TackettTcpServer +{ + + 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. + + ServerSocket serverSocket = new ServerSocket(2317); + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + Socket clientConnection = serverSocket.accept(); + InputStream is = clientConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String receivedMessage = br.readLine(); + System.out.println("Message received from client is " + receivedMessage); + + String returnMessage; + + returnMessage = "Enemy position acknowledged" + "\n"; + + + OutputStream os = clientConnection.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + BufferedWriter bw = new BufferedWriter(osw); + bw.write(returnMessage); + System.out.println("Message sent to the client is " + returnMessage); + bw.flush(); + + PrintStream ps = new PrintStream(os); + + ps.println("This was written by the server"); + + // 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 + " ))"); + + // 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"); + } + + } + +} \ No newline at end of file diff --git a/projects/Assignments/homework1/TackettUmlDiagram.pptx b/projects/Assignments/homework1/TackettUmlDiagram.pptx new file mode 100644 index 0000000000000000000000000000000000000000..b8a7b0ff57f16dcbebde4a4a6006eeb72fae5252 Binary files /dev/null and b/projects/Assignments/homework1/TackettUmlDiagram.pptx differ