diff --git a/.gitignore b/.gitignore index 29cc1942626e7a38c0a6f4c6b044bbf530c1186d..93bc79c92244b69254489592d8bce4a38e4e1cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ /projects/UdpExample1/UdpSender/nbproject/private/private.xml /projects/MulticastExample/MulticastSenderExample/nbproject/private/private.xml /projects/TcpExample1/nbproject/private/private.properties +/projects/Assignments/dist/ \ No newline at end of file diff --git a/projects/Assignments/2018JulySeptember/homework1/FurrAssignment1.java b/projects/Assignments/2018JulySeptember/homework1/FurrAssignment1.java deleted file mode 100644 index af7ca3436fe83017030cc54054356ded73ba07af..0000000000000000000000000000000000000000 --- a/projects/Assignments/2018JulySeptember/homework1/FurrAssignment1.java +++ /dev/null @@ -1,93 +0,0 @@ - -package FurrAssignment1; - -import java.io.*; -import java.net.*; - -/** - * Very slightly more complex than example1. 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 FurrAssignment1 -{ - - 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; // state - for(int i=2317; i<10; i++){ - ServerSocket serverSocket = new ServerSocket(i); - System.out.println("server established for port #" +i); - } - - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. - while(true) - { - Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed - - connectionCount++; // got another one! - - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server TcpExample2"); // to remote client - System.out.println("This server response was written by server TcpExample2"); // to server console - - // 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("got another connection, #" + connectionCount); // report progress - - // 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: " + e); - } - - } - -} diff --git a/projects/Assignments/2018JulySeptember/homework1/FurrAssignment2.java b/projects/Assignments/2018JulySeptember/homework1/FurrAssignment2.java index e7d8f9b7902eab69ec6c6450fd302bae99828b73..ab29b504d376f399d766e418db3a6da3590a6e32 100644 --- a/projects/Assignments/2018JulySeptember/homework1/FurrAssignment2.java +++ b/projects/Assignments/2018JulySeptember/homework1/FurrAssignment2.java @@ -31,56 +31,59 @@ public class FurrAssignment2 // 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[10]; int connectionCount = 0; // state - for(int i=2317; i<10; i++){ - ServerSocket serverSocket = new ServerSocket(i); - System.out.println("server established for port #" +i); + int j = 0; + for(int i=2317; j<10; i++){ + serverSocket[j] = new ServerSocket(i); + System.out.println("server established for port #" +i +"in Array slot" +j); + j++; } - + j=0; // Loop, infinitely, waiting for client connections. // Stop the program somewhere else. while(true) { - Socket clientConnection = serverSocket.accept(); // blocks! then proceeds once a connection is "accept"ed - - connectionCount++; // got another one! - - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server TcpExample2"); // to remote client - System.out.println("This server response was written by server TcpExample2"); // to server console - - // 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("got another connection, #" + connectionCount); // report progress - - // 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(); + try ( + Socket clientConnection = serverSocket.accept() // blocks! then proceeds once a connection is "accept"ed + ) { + connectionCount++; // got another one! + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server TcpExample2"); // to remote client + System.out.println("This server response was written by server TcpExample2"); // to server console + + // 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("got another connection, #" + connectionCount); // report progress + + // 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(); + } // got another one! } } catch(Exception e)