diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpClient.java index 0391b6f01d957f6e10689eb9d9abdfac9ee575ed..910cad6d6a30fa839dd683b12a967cab1c9ad1a6 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpClient.java @@ -1,72 +1,73 @@ package MV3500Cohort2018JanuaryMarch.homework1; -/* - * 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"); - } - -} +/* + * 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 { + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + 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/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpServer.java index 1f06beab781870e128818644a93ff8a47a70be5c..4f10a7bdec0da04af6f09ecc819fe7e4bc44e34b 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/BlankenbekerMyTcpServer.java @@ -1,97 +1,98 @@ package MV3500Cohort2018JanuaryMarch.homework1; -//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 [ipAddressOfServerLaptop] 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); - Socket clientConnection = serverSocket.accept(); - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. - while(true) - { - - - 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"); - } - - } - - - -} +//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 [ipAddressOfServerLaptop] 2317 + * + * And have him display the socket pairs he got. + * @author mcgredo + */ +public class BlankenbekerMyTcpServer +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + 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); + Socket clientConnection = serverSocket.accept(); + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + + + 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"); + } + + } + + + +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpClient.java index 0a1232189da6d2c311d3c2b27423e135ba4047bb..25d9cb3ea5ea7f2c7b73993418d2ceb2ba73869e 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpClient.java @@ -1,64 +1,67 @@ package MV3500Cohort2018JanuaryMarch.homework1; -/* - * 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 Homework1; - -import java.io.*; -import java.net.*; - -/** - * - * @author Brian - */ -public class HanleyTcpClient { - public static void main(String[] args) - { - try - { - 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("1 The message the server sent was " + serverMessage); - - // Outgoing Messages - OutputStream os = socket.getOutputStream(); - PrintStream ps = new PrintStream(os); - ps.println("bbb report: X, Y, X " + System.currentTimeMillis()); - - serverMessage = br.readLine(); - System.out.println("2 The message the server sent was " + serverMessage); - - //os = socket.getOutputStream(); - // ps = new PrintStream(os); - ps.println("Final message from client " + System.currentTimeMillis()); - serverMessage = br.readLine(); - System.out.println("3 The message the server sent was " + serverMessage); - - - } - catch(Exception e) - { - System.out.println(e); - System.out.println("Problem with client"); - } - - } -} +/* + * 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 Homework1; + +import java.io.*; +import java.net.*; + +/** + * + * @author Brian + */ +public class HanleyTcpClient +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + 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("1 The message the server sent was " + serverMessage); + + // Outgoing Messages + OutputStream os = socket.getOutputStream(); + PrintStream ps = new PrintStream(os); + ps.println("bbb report: X, Y, X " + System.currentTimeMillis()); + + serverMessage = br.readLine(); + System.out.println("2 The message the server sent was " + serverMessage); + + //os = socket.getOutputStream(); + // ps = new PrintStream(os); + ps.println("Final message from client " + System.currentTimeMillis()); + serverMessage = br.readLine(); + System.out.println("3 The message the server sent was " + serverMessage); + + + } + catch(Exception e) + { + System.out.println(e); + System.out.println("Problem with client"); + } + + } +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpServer.java index ec432a4f1fcf2df7d566b9c82faaefd82f431104..917eae54932634eef7344795eda6345507b60241 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/HanleyTcpServer.java @@ -1,106 +1,107 @@ package MV3500Cohort2018JanuaryMarch.homework1; -//package Homework1; - - -import java.io.*; -import java.net.*; - - - -/** - * - * @author Brian - */ -public class HanleyTcpServer { - -/** - * 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 [ipAddressOfServerLaptop] 2317 - * - * And have him display the socket pairs he got. - * @author mcgredo - */ - 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); - Socket clientConnection = serverSocket.accept(); - - // 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); - - 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 + " ))"); - - //Exchange of Data - InputStream is = clientConnection.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - - String clientMessage = br.readLine(); - - if (clientMessage.startsWith("loc")){ - System.out.println("Location Report \n"+ clientMessage); - } - else System.out.println("Incoming Message \n" + clientMessage); - ps.println("Message received"); - - // 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"); - } - - } - -} - +//package Homework1; + + +import java.io.*; +import java.net.*; + + + +/** + * + * @author Brian + */ +public class HanleyTcpServer { + +/** + * 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 [ipAddressOfServerLaptop] 2317 + * + * And have him display the socket pairs he got. + * @param args command-line arguments, string parameters (unused) + * @author mcgredo + */ + 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); + Socket clientConnection = serverSocket.accept(); + + // 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); + + 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 + " ))"); + + //Exchange of Data + InputStream is = clientConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String clientMessage = br.readLine(); + + if (clientMessage.startsWith("loc")){ + System.out.println("Location Report \n"+ clientMessage); + } + else System.out.println("Incoming Message \n" + clientMessage); + ps.println("Message received"); + + // 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"); + } + + } + +} + diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasClient2.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasClient2.java index 3ab74dee1dfe47b7a01e1de7f2f8036cbbfb4cf5..4fb3849a4f50e27ab13a57295684ca69c32d156e 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasClient2.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasClient2.java @@ -1,66 +1,66 @@ -/* - * 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 MV3500Cohort2018JanuaryMarch.homework1; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.util.Scanner; -/** - * - * @author Rico - */ -public class LandasClient2 { - - public static DataInputStream in; - public static DataOutputStream out; - - /** - * @param args command-line arguments - */ - public static void main(String[] args) { - // TODO code application logic here - //declare a scanner so we can write a message - Scanner keyboard = new Scanner(System.in); - - // localhost ip - String ip = "127.0.0.1"; - int port = 2317; - Socket socket; - - try { - - //connect - socket = new Socket(ip, port); - - //initialize streams - //DataInputStream in = new DataInputStream(socket.getInputStream()); - //DataOutputStream out = new DataOutputStream(socket.getOutputStream()); - in = new DataInputStream(socket.getInputStream()); - out = new DataOutputStream(socket.getOutputStream()); - - - while (true){ - System.out.print("\nMessage to server: "); - //Write a message - String message = keyboard.nextLine(); - //Send it to the server which will print it and send a confirmation - out.writeUTF(message); - - // recieve the incoming messages and print - String message2 = in.readUTF(); - System.out.println(message2); - } - - } - catch(IOException e) { - System.out.println(e); - System.out.println("\nProblem with client"); - } - } - -} +/* + * 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 MV3500Cohort2018JanuaryMarch.homework1; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Scanner; +/** + * + * @author Rico + */ +public class LandasClient2 { + + static DataInputStream in; + static DataOutputStream out; + + /** + * @param args command-line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + //declare a scanner so we can write a message + Scanner keyboard = new Scanner(System.in); + + // localhost ip + String ip = "127.0.0.1"; + int port = 2317; + Socket socket; + + try { + + //connect + socket = new Socket(ip, port); + + //initialize streams + //DataInputStream in = new DataInputStream(socket.getInputStream()); + //DataOutputStream out = new DataOutputStream(socket.getOutputStream()); + in = new DataInputStream(socket.getInputStream()); + out = new DataOutputStream(socket.getOutputStream()); + + + while (true){ + System.out.print("\nMessage to server: "); + //Write a message + String message = keyboard.nextLine(); + //Send it to the server which will print it and send a confirmation + out.writeUTF(message); + + // recieve the incoming messages and print + String message2 = in.readUTF(); + System.out.println(message2); + } + + } + catch(IOException e) { + System.out.println(e); + System.out.println("\nProblem with client"); + } + } + +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasServer2.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasServer2.java index 0314fced00c960226aafa617a6d841ad20555d1e..6fb1a854793d08407a618aae2c52563b81db1ada 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasServer2.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/LandasServer2.java @@ -1,68 +1,67 @@ -/* - * 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 MV3500Cohort2018JanuaryMarch.homework1; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.ServerSocket; -import java.net.Socket; - - -/** - * - * @author Rico - */ -public class LandasServer2 { - - public static DataInputStream in; - public static DataOutputStream out; - - /** - * @param args command-line arguments - */ - public static void main(String[] args) { - // TODO code application logic here - int port = 2317; - ServerSocket server; - Socket clientSocket; - - try { - - //start listening on port - server = new ServerSocket(port); - - System.out.println("Listening on port: " + port); - - //Accept client - clientSocket = server.accept(); - - System.out.println("Client Connected!"); - - //initialize streams so we can send message - in = new DataInputStream(clientSocket.getInputStream()); - out = new DataOutputStream(clientSocket.getOutputStream()); - - String message; - - while (true) { - - // as soon as a message is being received, print it out! - message = in.readUTF(); - System.out.println("Server received: " + message); - // send a message confirmation to the client - String temp1 = "Server: I received your message of " + "'" + message + "'"; - out.writeUTF(temp1); - - } - - } - catch(IOException e) { - System.out.println("problem with networking: " + e); - } - } - -} +/* + * 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 MV3500Cohort2018JanuaryMarch.homework1; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + + +/** + * + * @author Rico + */ +public class LandasServer2 { + + static DataInputStream in; + static DataOutputStream out; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) { + // TODO code application logic here + int port = 2317; + ServerSocket server; + Socket clientSocket; + + try { + + //start listening on port + server = new ServerSocket(port); + + System.out.println("Listening on port: " + port); + + //Accept client + clientSocket = server.accept(); + + System.out.println("Client Connected!"); + + //initialize streams so we can send message + in = new DataInputStream(clientSocket.getInputStream()); + out = new DataOutputStream(clientSocket.getOutputStream()); + + String message; + + while (true) { + + // as soon as a message is being received, print it out! + message = in.readUTF(); + System.out.println("Server received: " + message); + // send a message confirmation to the client + String temp1 = "Server: I received your message of " + "'" + message + "'"; + out.writeUTF(temp1); + + } + + } + catch(IOException e) { + System.out.println("problem with networking: " + e); + } + } + +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpClient.java index d4cba73960565370c080a5baec4869aab9c742e7..1a95b3311fce5cf709544a90e5d30f1e11ead24c 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpClient.java @@ -1,64 +1,64 @@ package MV3500Cohort2018JanuaryMarch.homework1; - -//package tcpclient; - -import java.io.*; -import java.net.*; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.Socket; -import java.util.Scanner; - -/** - * 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 MaroonTcpClient { - - public static DataInputStream input; - public static DataOutputStream output; - - - - public static void main(String[] args) - { - Scanner userInput = new Scanner(System.in); - try - { - System.out.println("creating socket"); - Socket socket = new Socket("localhost", 2317); - - input = new DataInputStream(socket.getInputStream()); - output = new DataOutputStream(socket.getOutputStream()); - - - - System.out.println("Type position or ID: \n "); - String request; - request = userInput.nextLine(); - output.writeUTF(request); - - String reply; - reply = input.readUTF(); - System.out.println(reply); - - - } - catch(Exception e) - { - System.out.println(e); - System.out.println("Problem with client"); - } - - } - -} + +//package tcpclient; + +import java.io.*; +import java.net.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Scanner; + +/** + * 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 MaroonTcpClient { + + static DataInputStream input; + static DataOutputStream output; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + Scanner userInput = new Scanner(System.in); + try + { + System.out.println("creating socket"); + Socket socket = new Socket("localhost", 2317); + + input = new DataInputStream(socket.getInputStream()); + output = new DataOutputStream(socket.getOutputStream()); + + + + System.out.println("Type position or ID: \n "); + String request; + request = userInput.nextLine(); + output.writeUTF(request); + + String reply; + reply = input.readUTF(); + System.out.println(reply); + + + } + catch(Exception e) + { + System.out.println(e); + System.out.println("Problem with client"); + } + + } + +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpServer.java index ca3c810813bb7193f4ce8c86700c6f306c0efc01..9efebe0aa0e0ae69ed6153d4a0ffe9c8d59ac8be 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/MaroonTcpServer.java @@ -1,89 +1,90 @@ package MV3500Cohort2018JanuaryMarch.homework1; -//package tcpserver; - -import java.io.*; -import java.net.*; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.Arrays; - -/** - * 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 [ipAddressOfServerLaptop] 2317 - * - * And have him display the socket pairs he got. - * @author mcgredo - */ -public class MaroonTcpServer -{ - - public static DataInputStream input; - public static DataOutputStream output; - - - public static void main(String[] args) - { - - MaroonUnit testUnit1 = new MaroonUnit (); - - 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(); - //OutputStream os = clientConnection.getOutputStream(); - input = new DataInputStream(clientConnection.getInputStream()); - output = new DataOutputStream(clientConnection.getOutputStream()); - - String request = input.readUTF(); - request = request.trim(); // get rid of outer whitespace - - if ("position".equalsIgnoreCase(request)){ - output.writeUTF(Arrays.toString(testUnit1.getPosition())); - } - else if ("ID".equalsIgnoreCase(request)){ - output.writeUTF(""+ testUnit1.getID()); - - }else - output.writeUTF("I don't understand, try www.google.com"); - - - // 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. - - clientConnection.close(); - } - } - catch(Exception e) - { - System.out.println("problem with networking"); - } - - } - +//package tcpserver; + +import java.io.*; +import java.net.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Arrays; + +/** + * 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 [ipAddressOfServerLaptop] 2317 + * + * And have him display the socket pairs he got. + * @author mcgredo + */ +public class MaroonTcpServer +{ + + static DataInputStream input; + static DataOutputStream output; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + + MaroonUnit testUnit1 = new MaroonUnit (); + + 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(); + //OutputStream os = clientConnection.getOutputStream(); + input = new DataInputStream(clientConnection.getInputStream()); + output = new DataOutputStream(clientConnection.getOutputStream()); + + String request = input.readUTF(); + request = request.trim(); // get rid of outer whitespace + + if ("position".equalsIgnoreCase(request)){ + output.writeUTF(Arrays.toString(testUnit1.getPosition())); + } + else if ("ID".equalsIgnoreCase(request)){ + output.writeUTF(""+ testUnit1.getID()); + + }else + output.writeUTF("I don't understand, try www.google.com"); + + + // 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. + + clientConnection.close(); + } + } + catch(Exception e) + { + System.out.println("problem with networking"); + } + + } + } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionClient.java index 519cf844ca49ba93da82f8b79d279df98dd957ca..6708391737ff6ed9b1b3013bf69e3530b14e6430 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionClient.java @@ -1,41 +1,41 @@ package MV3500Cohort2018JanuaryMarch.homework1; -//package PositionClient; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.Socket; -import java.net.UnknownHostException; - -/** - * - * @author AJSNELL - */ -public class SnellPositionClient { - /** - * @param args command-line arguments - * @throws java.io.IOException - */ - public static void main(String[] args) throws IOException { - String hostName = args[0]; - try (Socket clientSocket = new Socket(hostName, 8005); - PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); - BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { - String userInput; - out.println("unit id: 1\nunit pos: 11S MS 4859 9849"); - while ((userInput = stdIn.readLine()) != null) { - out.println(userInput); - System.out.println("from client: " + in.readLine()); - } - } catch (UnknownHostException e) { - System.err.println("Don't know about host " + hostName); - System.exit(1); - } catch (IOException e) { - System.err.println("Couldn't get I/O for the connection to " + hostName); - System.exit(1); - } - } +//package PositionClient; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.net.UnknownHostException; + +/** + * + * @author AJSNELL + */ +public class SnellPositionClient { + /** + * @param args command-line arguments, must include hostname + */ + public static void main(String[] args) + { + String hostName = args[0]; // poor practice, ought to throw meaningful exception message + try (Socket clientSocket = new Socket(hostName, 8005); + PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { + String userInput; + out.println("unit id: 1\nunit pos: 11S MS 4859 9849"); + while ((userInput = stdIn.readLine()) != null) { + out.println(userInput); + System.out.println("from client: " + in.readLine()); + } + } catch (UnknownHostException e) { + System.err.println("Don't know about host " + hostName); + System.exit(1); + } catch (IOException e) { + System.err.println("Couldn't get I/O for the connection to " + hostName); + System.exit(1); + } + } } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionServer.java index ed6c9b389b538b58a21d68dfdbaed585106b7501..39a6259fb519dd4dbf08b03c2d8433a3a3af178d 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/SnellPositionServer.java @@ -1,32 +1,32 @@ package MV3500Cohort2018JanuaryMarch.homework1; -//package positionserver; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.ServerSocket; -import java.net.Socket; -import java.io.PrintWriter; - -public class SnellPositionServer { - /** - * @param args command-line arguments - * @throws java.io.IOException - */ - public static void main(String[] args) throws IOException { - try (ServerSocket serverSocket = new ServerSocket(8005); - Socket clientSocket = serverSocket.accept(); - PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); - BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) { - System.out.println("Client connected on port 8005"); - String inputLine; - while ((inputLine = in.readLine()) != null) { - System.out.println("Received message: " + inputLine + " from " + clientSocket.toString()); - out.println(inputLine); - } - } catch (IOException e) { - System.out.println("Exception when trying to listen on port 8005"); - } - } +//package positionserver; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; +import java.io.PrintWriter; + +public class SnellPositionServer { + /** + * @param args command-line arguments + */ + public static void main(String[] args) + { + try (ServerSocket serverSocket = new ServerSocket(8005); + Socket clientSocket = serverSocket.accept(); + PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) { + System.out.println("Client connected on port 8005"); + String inputLine; + while ((inputLine = in.readLine()) != null) { + System.out.println("Received message: " + inputLine + " from " + clientSocket.toString()); + out.println(inputLine); + } + } catch (IOException e) { + System.out.println("Exception when trying to listen on port 8005"); + } + } } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpClient.java index 088ed611314aceca9e8d17e0df7da4dc00d56783..4cd6cc2e8d7cba39634e6d99520d801ecc2e9410 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpClient.java @@ -16,11 +16,14 @@ import java.net.*; * * @author mcgredo */ -public class TackettTcpClient { - +public class TackettTcpClient +{ + /** socket parameter of interest */ 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 + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpServer.java index edb1f40ca73a9bd6503caaeb32514b6a7018f8a3..d1f484b63ff3a293fdd2d8f4579895c421135fc7 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/TackettTcpServer.java @@ -27,7 +27,8 @@ import java.net.*; */ public class TackettTcpServer { - + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpClient.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpClient.java index 0e58f3dd9ab4c4e7cc3ca73cf103bd79cad6d4d1..bf8eb6d969a42286b651ddf82b200bb637695eda 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpClient.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpClient.java @@ -13,8 +13,10 @@ import java.net.*; * @version 20180212 * */ -public class YamashitaDeMouraTcpClient { - +public class YamashitaDeMouraTcpClient +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpServer.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpServer.java index d2c7a701f461528ea059c7d9757f31d7faee5526..38061bcc63984ffda7be5c235e3f4d2932f58d39 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpServer.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework1/YamashitaDeMouraTcpServer.java @@ -17,7 +17,8 @@ import java.net.*; */ public class YamashitaDeMouraTcpServer { - + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/AyresAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/AyresAssignment1.java index 5e55a3481feecc71d08120c557af75a08f711d3d..08888f83a671a09993d7f237a8f4608851d7a43f 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/AyresAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/AyresAssignment1.java @@ -14,7 +14,8 @@ import java.net.*; */ public class AyresAssignment1 { - + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/CainAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/CainAssignment1.java index f933ce94c0f3e73295241631b2d157b47546d4a8..21268d63875bc84c7e23203632ab6807b3af28a0 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/CainAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/CainAssignment1.java @@ -12,8 +12,10 @@ import java.net.*; * ask for the ip address of the server * <code>telnet ipOfServersLaptop 2318</code> */ -public class CainAssignment1 { - +public class CainAssignment1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { System.out.println("Hello invoker, this server process is now running"); // reporting for duty diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment1.java index ce03b13e173058abade400180ea73435cefeddc8..2ca1e218a19507eb70f4b405ea5f336b326a23b0 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment1.java @@ -1,53 +1,56 @@ -package MV3500Cohort2018JulySeptember.homework1; - - -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/* - * 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. - */ - -/** - * - * @author ekdem - */ -public class DemchkoAssignment1 { - public static void main(String[] args) - { - try - { - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // The Socket object represents the connection between - // the server and client, including a full duplex - // connection - Socket clientConnection = serverSocket.accept(); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server Demchko"); // to remote client - System.out.println("This server response was written by server Demchko"); // to server console - ps.println("HelloWorld"); - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - - clientConnection.close(); - } - catch(Exception error) - { - System.out.println("Nope, try again because " + error); - } - } - -} +package MV3500Cohort2018JulySeptember.homework1; + + +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/* + * 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. + */ + +/** + * + * @author ekdem + */ +public class DemchkoAssignment1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // The Socket object represents the connection between + // the server and client, including a full duplex + // connection + Socket clientConnection = serverSocket.accept(); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server Demchko"); // to remote client + System.out.println("This server response was written by server Demchko"); // to server console + ps.println("HelloWorld"); + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + + clientConnection.close(); + } + catch(Exception error) + { + System.out.println("Nope, try again because " + error); + } + } + +} diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java index ab35b05bab4136bbf5f96aec558182d02b05bfef..2ff1197d588b0ddbf8c023dec23515fc942803e5 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java @@ -1,61 +1,62 @@ -package MV3500Cohort2018JulySeptember.homework1; - -import java.io.*; -import java.net.*; - - -public class DemchkoAssignment2 -{ - - public static void main(String[] args) throws IOException - { - try - { - ServerSocket[] serverSocket = new ServerSocket[9]; - int connectionCount = 0; - int j = 0; - for(int i=2317; j<9; i++){ - serverSocket[j] = new ServerSocket(i); - System.out.println("server in port " +i +" in array position " +j); //changed - j++; - } - j=0; - - while(true) - { - try ( - Socket clientConnection = serverSocket[j].accept(); - ) - { - connectionCount++; - - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server DemchkoAssignment2"); //changed - System.out.println("This server response was written by server DemchkoAssignment2"); //changed - - InetAddress localAddress = clientConnection.getLocalAddress(); - InetAddress remoteAddress = clientConnection.getInetAddress(); - - int localPort = clientConnection.getLocalPort(); - int remotePort = clientConnection.getPort(); - - System.out.println("The socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + - remoteAddress.toString() + ", " + remotePort + " ))"); - - System.out.println("next connection, #" + connectionCount); // report progress - j++; - - ps.flush(); - } - } - } - catch(Exception error) - { - System.out.println("nope, not today: " + error); - } - - } - +package MV3500Cohort2018JulySeptember.homework1; + +import java.io.*; +import java.net.*; + + +public class DemchkoAssignment2 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + ServerSocket[] serverSocket = new ServerSocket[9]; + int connectionCount = 0; + int j = 0; + for(int i=2317; j<9; i++){ + serverSocket[j] = new ServerSocket(i); + System.out.println("server in port " +i +" in array position " +j); //changed + j++; + } + j=0; + + while(true) + { + try ( + Socket clientConnection = serverSocket[j].accept(); + ) + { + connectionCount++; + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server DemchkoAssignment2"); //changed + System.out.println("This server response was written by server DemchkoAssignment2"); //changed + + InetAddress localAddress = clientConnection.getLocalAddress(); + InetAddress remoteAddress = clientConnection.getInetAddress(); + + int localPort = clientConnection.getLocalPort(); + int remotePort = clientConnection.getPort(); + + System.out.println("The socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + System.out.println("next connection, #" + connectionCount); // report progress + j++; + + ps.flush(); + } + } + } + catch(Exception error) + { + System.out.println("nope, not today: " + error); + } + + } + } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FriscoAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FriscoAssignment1.java index 8b6f946734aeb158b4294cc2da41860fa5d3c381..496a65671e39774bbd902471195a0b0f2d57720d 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FriscoAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FriscoAssignment1.java @@ -1,65 +1,66 @@ -package MV3500Cohort2018JulySeptember.homework1; - -import java.io.*; -import java.net.*; - -/** - * The simplest possible TCP network program. It listens for - * a connection, from telnet (telnet localhost 2317) or a program - * you write, which we will do later. Right now the TcpExample simply - * writes a string in response to a connection. - * - * Testing the running server program from telnet looks like this: - * - * it154916:projects mcgredo$ telnet localhost 2317 - * Trying ::1... - * Connected to localhost. - * Escape character is '^]'. - * This was written by the server - * Connection closed by foreign host. - * - * Notice that "This was written by the server" matches - * what is written by the code below, over the output stream. - * - * After this first connection the program below drops out - * the bottom of the program, and does not repeat itself. - * The program exits. - * - * @author mcgredo - */ -public class FriscoAssignment1 -{ - - public static void main(String[] args) - { - try - { - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // The Socket object represents the connection between - // the server and client, including a full duplex - // connection - Socket clientConnection = serverSocket.accept(); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server FriscoAssignment1"); // to remote client - System.out.println("This server response was written by server FriscoAssignment1"); // to server console - ps.println("No time to say 'Hello, Goodbye' I'm late, I'm late, I'm late"); - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - - clientConnection.close(); - } - catch(Exception e) - { - System.out.println("I am sorry Neo but you took the 'Red pill': " + e); - } - } -} +package MV3500Cohort2018JulySeptember.homework1; + +import java.io.*; +import java.net.*; + +/** + * The simplest possible TCP network program. It listens for + * a connection, from telnet (telnet localhost 2317) or a program + * you write, which we will do later. Right now the TcpExample simply + * writes a string in response to a connection. + * + * Testing the running server program from telnet looks like this: + * + * it154916:projects mcgredo$ telnet localhost 2317 + * Trying ::1... + * Connected to localhost. + * Escape character is '^]'. + * This was written by the server + * Connection closed by foreign host. + * + * Notice that "This was written by the server" matches + * what is written by the code below, over the output stream. + * + * After this first connection the program below drops out + * the bottom of the program, and does not repeat itself. + * The program exits. + * + * @author mcgredo + */ +public class FriscoAssignment1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // The Socket object represents the connection between + // the server and client, including a full duplex + // connection + Socket clientConnection = serverSocket.accept(); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server FriscoAssignment1"); // to remote client + System.out.println("This server response was written by server FriscoAssignment1"); // to server console + ps.println("No time to say 'Hello, Goodbye' I'm late, I'm late, I'm late"); + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + + clientConnection.close(); + } + catch(Exception e) + { + System.out.println("I am sorry Neo but you took the 'Red pill': " + e); + } + } +} diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment1.java index 85a3b02d3507e7fcc050c655a478af4abf43571d..3664892714892880bb89d4e0d0ae0fcb45336723 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment1.java @@ -29,7 +29,8 @@ import java.net.*; */ public class FurrAssignment1 { - + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java index f3c1ac73de0bdcb162e8b1f84950dbce6c38ef14..e7539db6d46db46600194048c6c5a128a73e153f 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java @@ -31,8 +31,9 @@ import java.net.*; */ public class FurrAssignment2 { - - public static void main(String[] args) throws IOException + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) throws IOException { try { diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/JacksonAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/JacksonAssignment1.java index 57a4e83e289186f71f6108b2d787292627a67d6f..216bfe6e7648f685b5d16752d731b64f23bd1bfa 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/JacksonAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/JacksonAssignment1.java @@ -15,9 +15,10 @@ import java.net.Socket; * * @author John */ -public class JacksonAssignment1 { - - +public class JacksonAssignment1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { System.out.println("Hello invoker, this server process is now running"); // reporting for duty diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/ThomersonAssignment1.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/ThomersonAssignment1.java index ff2c35f3015511421309d4b439fabfc9e826d531..bbf7a5c0db12bee1f82e34f9a91bd9350422ba70 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/ThomersonAssignment1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/ThomersonAssignment1.java @@ -47,8 +47,10 @@ import java.net.*; * * @author courtneythomerson */ -public class ThomersonAssignment1 { - +public class ThomersonAssignment1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpClient.java b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpClient.java index 9563f2412e7c353c7f7fbaa9494119f28cb2eb8d..8b35b3f3af4cab68826b8e82295113cbce51574b 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpClient.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpClient.java @@ -17,7 +17,7 @@ public class FurrTcpClient { public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 /** run the program - * @param args unused string parameters */ + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { boolean openConnection = true; try { diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpServer.java b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpServer.java index 4ada54ae29817d015f0da67b2f7bedc7132924f8..8c5ecb869734732252ecd35d665ba72cf7812327 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpServer.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Furr/FurrTcpServer.java @@ -21,9 +21,9 @@ import java.net.*; public class FurrTcpServer { /** run the program - * @param args unused string parameters */ - public static void main(String[] args) throws IOException { - + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { String[] responses = new String[10]; //canned responses for multiple inputs from client responses[0] = "Hello"; responses[1] = "Are you there?"; @@ -50,12 +50,12 @@ public class FurrTcpServer { // 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(); // 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) { - + while (connectionActive) + { OutputStream os = clientConnection.getOutputStream(); PrintStream ps = new PrintStream(os); ps.println("This was written by the server"); @@ -102,9 +102,8 @@ public class FurrTcpServer { connectionActive = false; ps.flush(); clientConnection.close(); // like it or not, you're outta here! + } } - } - } catch (IOException e) { System.out.println("problem with networking" + e); } diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Receiver.java b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Receiver.java index 5ad03fb94308d775f8e1526d08b3981a8bdfabaa..1c4b4df7c2b016dc254d2516ffe39e2c9f8fe520 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Receiver.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Receiver.java @@ -26,7 +26,7 @@ public class FurrFriscoHw3Receiver { public static final int TTL = 10; /** run the program - * @param args unused string parameters */ + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Sender.java b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Sender.java index f8aa17e06e3d531342dcdc497bb4d07c831ded09..9bb743cf1477566ae7da1d949aec1edb3c47cd2c 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Sender.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Furr_Frisco_Homework3/FurrFriscoHw3Sender.java @@ -25,7 +25,7 @@ public class FurrFriscoHw3Sender { public static final int TTL = 10; /** run the program - * @param args unused string parameters */ + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try { diff --git a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/OBSSendRecieve1.java b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/OBSSendRecieve1.java index c68ce7c6b6dd42288feac524b1009f2fc09956bb..9626b7feb798883e5d8842296018f77b43cdd19c 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/OBSSendRecieve1.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/OBSSendRecieve1.java @@ -1,279 +1,281 @@ -package MV3500Cohort2018JulySeptember.projects.FriscoFurr; - -import java.net.*; -import java.io.*; -import java.util.*; -import edu.nps.moves.dis.*; // OpenDIS version 4 -import java.io.IOException; -import edu.nps.moves.disutil.PduFactory; -import edu.nps.moves.disutil.DisTime; - -public class OBSSendRecieve1 { - - /** - * Default multicast group address we send on. - */ - public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; - - /** - * Default multicast port used, matches Wireshark DIS capture default - */ - public static final int DEFAULT_MULTICAST_PORT = 3000; - - private int port; - InetAddress multicastAddress; - - public static final int MULTICAST_PORT = 3000; - public static final String MULTICAST_GROUP = "239.1.2.3"; - public static final boolean USE_FAST_ESPDU = false; - public long[] sentBuffer = new long[100]; - public static List sentBufferList; - DisTime disTime = DisTime.getInstance(); - int transmission =1; - - /** - * Constructor just got to construct. - * @param port - * @param multicast - */ - public OBSSendRecieve1(int port, String multicast) { - this.sentBufferList = new ArrayList<>(); - try { - this.port = port; - multicastAddress = InetAddress.getByName(multicast); - if (!multicastAddress.isMulticastAddress()) { - System.out.println("Not a multicast address: " + multicast); - } - } catch (UnknownHostException e) { - System.out.println("Unable to open socket: " + e); - } - } - - /** - * This would be the sending Run method. Takes in several PDUs and for each one has a switch statement ready for it. - * @param pdupass - * @throws UnknownHostException unable to reach host address - * @throws IOException input-output error - */ - public void run(Pdu... pdupass) throws UnknownHostException, IOException { - - List<Pdu> generatedPdus = new ArrayList<>(); - Pdu aPdu = null; - if(transmission ==1){ - System.out.println("\nInitalizing OP coms..."); - transmission++; - } - if(transmission>1){ - System.out.println("\nObserver Sending traffic..."); - } - // Send the PDUs we created - InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); - MulticastSocket socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); - socket.joinGroup(localMulticastAddress); - - for (Pdu i : pdupass) { - Pdu pdu = i; - if (sentBufferList.contains(pdu.getTimestamp())) { - break; - } - - short currentPduType = pdu.getPduType(); - System.out.println("in observer sender, processing PDU type: " + currentPduType); - - switch (currentPduType) // using enumeration values from edu.nps.moves.disenum.* - { - - case 1: //ENTITY_STATE: - aPdu = pdu; - break; - - case 16: //ACTION_REQUEST: - aPdu = new ActionRequestPdu(); - break; - - case 22: //CommentPdu - aPdu = pdu; - break; - - default: - System.out.print("PDU of type " + pdu + " not supported by Observer, created or sent "); - System.out.println(); - } - if (aPdu != null) { - generatedPdus.add(aPdu); - System.out.println("APDU container count " + generatedPdus.size()); - } - - } - for (int idx = 0; idx < generatedPdus.size(); idx++) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - byte[] buffer; - - aPdu = generatedPdus.get(idx); - aPdu.marshal(dos); - - buffer = baos.toByteArray(); - DatagramPacket packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT); - socket.send(packet); - sentBufferList.add(aPdu.getTimestamp()); - System.out.println("Observer Sent PDU of type " + aPdu.getClass().getName()+"\n"); - } - } - - /** - * Main function takes no specific arguments, but is the recieving portion of the code. Once it hears a specific type of PDU it creates another and sends it to the Run function - * called sender created on line 136 (2nd statement in the main function) - * @param args command-line arguments - * @throws IOException input-output error - */ - public static void main(String[] args) throws IOException { - DisTime disTime = DisTime.getInstance(); - - // - // Inital Hello world from entity: - // - OBSSendRecieve1 sender = new OBSSendRecieve1(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); //initalize the sender - - EntityStatePdu OBSespdu = new EntityStatePdu(); - Marking marking = new Marking(); - marking.setCharactersString("Observer"); - OBSespdu.setMarking(marking); - OBSespdu.setExerciseID((short) 1); - EntityID OBSID = OBSespdu.getEntityID(); - OBSID.setSite(1); // 0 is apparently not a valid site number, per the spec - OBSID.setApplication(1); - OBSID.setEntity(2); - EntityType entityType = OBSespdu.getEntityType(); - // - //Need to update the info below to match the unit type IAW SISO-REF-010-2015 V.21 - // https://www.sisostds.org/DesktopModules/Bring2mind/DMX/Download.aspx?Command=Core_Download&EntryId=42916&PortalId=0&TabId=105 - // - entityType.setEntityKind((short) 1); // Platform (vs lifeform, munition, sensor, etc.) TGT=1, OBS = 1 - entityType.setCountry(225); // USA TGT = 222 (Russia), OBS = 225 - entityType.setDomain((short) 1); // Land (vs air, surface, subsurface, space) both TGT and OBS are 1 - entityType.setCategory((short) 40); // FDC TGT = 1, Tank OBS=40 OP -// entityType.setSubcategory((short) 12); // M1068 TGT = 2, T72 tank NONE FOR OP -// entityType.setSpec((short) 1); // M1068 w/ SICUP Tent NONE FOR TGT OR OP - Vector3Double location = new Vector3Double(); - location.setX(75.0); //TGT = 150 OBS = 75 - location.setY(75.0); //TGT = 150 OBS = 75 - location.setZ(50.0); //TGT = 20 OBS = 50 - OBSespdu.setEntityLocation(location); - int timestamp = disTime.getDisAbsoluteTimestamp(); - OBSespdu.setTimestamp(timestamp); - sender.run(OBSespdu); //sends inital here I am and who I am - - //other player to look out for: - EntityID FDCEntityID = new EntityID(); - FDCEntityID.setEntity(1); - FDCEntityID.setApplication(1); - FDCEntityID.setSite(1); - - EntityID TGTEntityID = new EntityID(); - TGTEntityID.setEntity(3); - TGTEntityID.setApplication(1); - TGTEntityID.setSite(1); - - /* BELOW IS THE RECIEVE CODE // - // // - // // - */ // - PduFactory factory; - MulticastSocket socket = null; - InetAddress address = null; - DatagramPacket packet; - short currentPduType; //will use the curentPduType as the check for sending other packets. - - try { - System.out.println("Observer is in the OP and looking for targets...\n\n"); - socket = new MulticastSocket(MULTICAST_PORT); - address = InetAddress.getByName(MULTICAST_GROUP); - socket.joinGroup(address); - - factory = new PduFactory(); - - while (true) // Loop infinitely, receiving datagrams - { - byte buffer[] = new byte[1500]; // typical MTU size - - packet = new DatagramPacket(buffer, buffer.length); // reset - - socket.receive(packet); - - String marking2 = new String(); - Pdu pdu = factory.createPdu(packet.getData()); - currentPduType = pdu.getPduType(); - if (currentPduType == 14) //stop/freeze PDU - { - System.out.println("recieved Stop/Freeze packet..."); - return; - } - if (pdu != null) { - if (sentBufferList.contains(pdu.getTimestamp())) { - pdu = null; - } - if (pdu != null) { - - String currentPduTypeName = pdu.getClass().getName(); - if (currentPduType == 1) { - EntityStatePdu pdu2 = (EntityStatePdu) pdu; - marking2 = pdu2.getMarking().getCharactersString(); - } - - StringBuilder message = new StringBuilder(); - message.append("received DIS PDU: "); - message.append("pduType "); - if (currentPduType < 10) { - message.append(" "); - } - message.append(currentPduType).append(" ").append(currentPduTypeName); - message.append(" "); - message.append(marking2); - System.out.println(message.toString()); - //Reference for PDU Types: - // http://faculty.nps.edu/brutzman/vrtp/mil/navy/nps/disEnumerations/JdbeHtmlFiles/pdu/8.htm - // - - if (currentPduType == 1) //EntityState - { - EntityStatePdu entityPDU = (EntityStatePdu) pdu; - EntityType PduEntityType = entityPDU.getEntityType(); - if (PduEntityType.getCountry() == 222) { - ActionRequestPdu action = new ActionRequestPdu(); - action.setExerciseID((short) 1); - action.setRequestID((long) 2); - action.setOriginatingEntityID(OBSID); - action.setReceivingEntityID(FDCEntityID); - timestamp = disTime.getDisAbsoluteTimestamp(); - action.setTimestamp(timestamp); - System.out.println("\n Got a Russian SOB! Preparing CFF to send."); - sender.run(action); - } - } - - if (currentPduType == 3) //Detination - { - CommentPdu comment = new CommentPdu(); - comment.setExerciseID((short) 1); - comment.setOriginatingEntityID(TGTEntityID); - timestamp = disTime.getDisAbsoluteTimestamp(); - comment.setTimestamp(timestamp); - sender.run(comment); - } - - } else { - System.out.println("received packet but pdu is null and originated from Observer. Still searching for other Targets..."); - } - } - } - } catch (IOException e) { - System.out.println("Problem with Observer.PduReceiver, see exception trace:"); - System.out.println(e); - } finally { - System.out.println("Observer.PduReceiver complete. - OUT!"); - } - - } - -} +package MV3500Cohort2018JulySeptember.projects.FriscoFurr; + +import java.net.*; +import java.io.*; +import java.util.*; +import edu.nps.moves.dis.*; // OpenDIS version 4 +import java.io.IOException; +import edu.nps.moves.disutil.PduFactory; +import edu.nps.moves.disutil.DisTime; + +public class OBSSendRecieve1 { + + /** + * Default multicast group address we send on. + */ + public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; + + /** + * Default multicast port used, matches Wireshark DIS capture default + */ + public static final int DEFAULT_MULTICAST_PORT = 3000; + + private int port; + InetAddress multicastAddress; + + /** socket value of shared interest */ + public static final int MULTICAST_PORT = 3000; + /** socket value of shared interest */ + public static final String MULTICAST_GROUP = "239.1.2.3"; + private static final boolean USE_FAST_ESPDU = false; + private long[] sentBuffer = new long[100]; + private static List sentBufferList; + DisTime disTime = DisTime.getInstance(); + int transmission =1; + + /** + * Constructor just got to construct. + * @param port of interest + * @param multicast address of interest + */ + public OBSSendRecieve1(int port, String multicast) { + this.sentBufferList = new ArrayList<>(); + try { + this.port = port; + multicastAddress = InetAddress.getByName(multicast); + if (!multicastAddress.isMulticastAddress()) { + System.out.println("Not a multicast address: " + multicast); + } + } catch (UnknownHostException e) { + System.out.println("Unable to open socket: " + e); + } + } + + /** + * This would be the sending Run method. Takes in several PDUs and for each one has a switch statement ready for it. + * @param pdupass + * @throws UnknownHostException unable to reach host address + * @throws IOException input-output error + */ + public void run(Pdu... pdupass) throws UnknownHostException, IOException { + + List<Pdu> generatedPdus = new ArrayList<>(); + Pdu aPdu = null; + if(transmission ==1){ + System.out.println("\nInitalizing OP coms..."); + transmission++; + } + if(transmission>1){ + System.out.println("\nObserver Sending traffic..."); + } + // Send the PDUs we created + InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS); + MulticastSocket socket = new MulticastSocket(DEFAULT_MULTICAST_PORT); + socket.joinGroup(localMulticastAddress); + + for (Pdu i : pdupass) { + Pdu pdu = i; + if (sentBufferList.contains(pdu.getTimestamp())) { + break; + } + + short currentPduType = pdu.getPduType(); + System.out.println("in observer sender, processing PDU type: " + currentPduType); + + switch (currentPduType) // using enumeration values from edu.nps.moves.disenum.* + { + + case 1: //ENTITY_STATE: + aPdu = pdu; + break; + + case 16: //ACTION_REQUEST: + aPdu = new ActionRequestPdu(); + break; + + case 22: //CommentPdu + aPdu = pdu; + break; + + default: + System.out.print("PDU of type " + pdu + " not supported by Observer, created or sent "); + System.out.println(); + } + if (aPdu != null) { + generatedPdus.add(aPdu); + System.out.println("APDU container count " + generatedPdus.size()); + } + + } + for (int idx = 0; idx < generatedPdus.size(); idx++) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + byte[] buffer; + + aPdu = generatedPdus.get(idx); + aPdu.marshal(dos); + + buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT); + socket.send(packet); + sentBufferList.add(aPdu.getTimestamp()); + System.out.println("Observer Sent PDU of type " + aPdu.getClass().getName()+"\n"); + } + } + + /** + * Main function takes no specific arguments, but is the recieving portion of the code. Once it hears a specific type of PDU it creates another and sends it to the Run function + * called sender created on line 136 (2nd statement in the main function) + * @param args command-line arguments + * @throws IOException input-output error + */ + public static void main(String[] args) throws IOException { + DisTime disTime = DisTime.getInstance(); + + // + // Inital Hello world from entity: + // + OBSSendRecieve1 sender = new OBSSendRecieve1(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); //initalize the sender + + EntityStatePdu OBSespdu = new EntityStatePdu(); + Marking marking = new Marking(); + marking.setCharactersString("Observer"); + OBSespdu.setMarking(marking); + OBSespdu.setExerciseID((short) 1); + EntityID OBSID = OBSespdu.getEntityID(); + OBSID.setSite(1); // 0 is apparently not a valid site number, per the spec + OBSID.setApplication(1); + OBSID.setEntity(2); + EntityType entityType = OBSespdu.getEntityType(); + // + //Need to update the info below to match the unit type IAW SISO-REF-010-2015 V.21 + // https://www.sisostds.org/DesktopModules/Bring2mind/DMX/Download.aspx?Command=Core_Download&EntryId=42916&PortalId=0&TabId=105 + // + entityType.setEntityKind((short) 1); // Platform (vs lifeform, munition, sensor, etc.) TGT=1, OBS = 1 + entityType.setCountry(225); // USA TGT = 222 (Russia), OBS = 225 + entityType.setDomain((short) 1); // Land (vs air, surface, subsurface, space) both TGT and OBS are 1 + entityType.setCategory((short) 40); // FDC TGT = 1, Tank OBS=40 OP +// entityType.setSubcategory((short) 12); // M1068 TGT = 2, T72 tank NONE FOR OP +// entityType.setSpec((short) 1); // M1068 w/ SICUP Tent NONE FOR TGT OR OP + Vector3Double location = new Vector3Double(); + location.setX(75.0); //TGT = 150 OBS = 75 + location.setY(75.0); //TGT = 150 OBS = 75 + location.setZ(50.0); //TGT = 20 OBS = 50 + OBSespdu.setEntityLocation(location); + int timestamp = disTime.getDisAbsoluteTimestamp(); + OBSespdu.setTimestamp(timestamp); + sender.run(OBSespdu); //sends inital here I am and who I am + + //other player to look out for: + EntityID FDCEntityID = new EntityID(); + FDCEntityID.setEntity(1); + FDCEntityID.setApplication(1); + FDCEntityID.setSite(1); + + EntityID TGTEntityID = new EntityID(); + TGTEntityID.setEntity(3); + TGTEntityID.setApplication(1); + TGTEntityID.setSite(1); + + /* BELOW IS THE RECIEVE CODE // + // // + // // + */ // + PduFactory factory; + MulticastSocket socket = null; + InetAddress address = null; + DatagramPacket packet; + short currentPduType; //will use the curentPduType as the check for sending other packets. + + try { + System.out.println("Observer is in the OP and looking for targets...\n\n"); + socket = new MulticastSocket(MULTICAST_PORT); + address = InetAddress.getByName(MULTICAST_GROUP); + socket.joinGroup(address); + + factory = new PduFactory(); + + while (true) // Loop infinitely, receiving datagrams + { + byte buffer[] = new byte[1500]; // typical MTU size + + packet = new DatagramPacket(buffer, buffer.length); // reset + + socket.receive(packet); + + String marking2 = new String(); + Pdu pdu = factory.createPdu(packet.getData()); + currentPduType = pdu.getPduType(); + if (currentPduType == 14) //stop/freeze PDU + { + System.out.println("recieved Stop/Freeze packet..."); + return; + } + if (pdu != null) { + if (sentBufferList.contains(pdu.getTimestamp())) { + pdu = null; + } + if (pdu != null) { + + String currentPduTypeName = pdu.getClass().getName(); + if (currentPduType == 1) { + EntityStatePdu pdu2 = (EntityStatePdu) pdu; + marking2 = pdu2.getMarking().getCharactersString(); + } + + StringBuilder message = new StringBuilder(); + message.append("received DIS PDU: "); + message.append("pduType "); + if (currentPduType < 10) { + message.append(" "); + } + message.append(currentPduType).append(" ").append(currentPduTypeName); + message.append(" "); + message.append(marking2); + System.out.println(message.toString()); + //Reference for PDU Types: + // http://faculty.nps.edu/brutzman/vrtp/mil/navy/nps/disEnumerations/JdbeHtmlFiles/pdu/8.htm + // + + if (currentPduType == 1) //EntityState + { + EntityStatePdu entityPDU = (EntityStatePdu) pdu; + EntityType PduEntityType = entityPDU.getEntityType(); + if (PduEntityType.getCountry() == 222) { + ActionRequestPdu action = new ActionRequestPdu(); + action.setExerciseID((short) 1); + action.setRequestID((long) 2); + action.setOriginatingEntityID(OBSID); + action.setReceivingEntityID(FDCEntityID); + timestamp = disTime.getDisAbsoluteTimestamp(); + action.setTimestamp(timestamp); + System.out.println("\n Got a Russian SOB! Preparing CFF to send."); + sender.run(action); + } + } + + if (currentPduType == 3) //Detination + { + CommentPdu comment = new CommentPdu(); + comment.setExerciseID((short) 1); + comment.setOriginatingEntityID(TGTEntityID); + timestamp = disTime.getDisAbsoluteTimestamp(); + comment.setTimestamp(timestamp); + sender.run(comment); + } + + } else { + System.out.println("received packet but pdu is null and originated from Observer. Still searching for other Targets..."); + } + } + } + } catch (IOException e) { + System.out.println("Problem with Observer.PduReceiver, see exception trace:"); + System.out.println(e); + } finally { + System.out.println("Observer.PduReceiver complete. - OUT!"); + } + + } + +} diff --git a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/TGTSendReceive.java b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/TGTSendReceive.java index 881c158c804ecbb5515b56ada141e94836bdf960..62aee3b78012cba8eb526f67394de04d85d21a5e 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/TGTSendReceive.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/TGTSendReceive.java @@ -8,6 +8,7 @@ import java.io.IOException; import edu.nps.moves.disutil.PduFactory; import edu.nps.moves.disutil.DisTime; +/** Target send and receive */ public class TGTSendReceive { /** @@ -35,8 +36,8 @@ public class TGTSendReceive /** * Constructor just got to construct. - * @param port - * @param multicast + * @param port TCP port of interest + * @param multicast address */ public TGTSendReceive(int port, String multicast) { this.sentBufferList = new ArrayList<>(); diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java index 0200262372992fddecddd0e2899decc8cc41a01b..df588da9f564c94485c5eddd39d8413222406f3b 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BoronTcpExample1Telnet1.java @@ -32,6 +32,8 @@ import java.sql.Timestamp; */ public class BoronTcpExample1Telnet1 { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrennenstuhlTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrennenstuhlTcpExample1Telnet.java index 719f56e4050b0ff4365ec91e0314596798fda8ba..8aced5a42edc752256e6d68f51c811fbf9c6f509 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrennenstuhlTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrennenstuhlTcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class BrennenstuhlTcpExample1Telnet { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java index f7cfe80e4ceff8694d048152f7d390a13c388efc..508d62e88eb91f712763feb8be6abd3885ab2476 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/BrutzmanRefactorTcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class BrutzmanRefactorTcpExample1Telnet { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/FetterolfTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/FetterolfTcpExample1Telnet.java index f5db99db7d3346a54b88e9a6c5818d5579cc44cd..25c1d0f02cad7d760d6f074699708433c4c6cbcb 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/FetterolfTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/FetterolfTcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class FetterolfTcpExample1Telnet { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/KNOBELOCH_TcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/KNOBELOCH_TcpExample1Telnet.java index f36c1c43a71c96c7e5241c294e93b15c9644f987..9339ca85ca8e5420745d00eabc38988696aa3b70 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/KNOBELOCH_TcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/KNOBELOCH_TcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class KNOBELOCH_TcpExample1Telnet { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java index 54f7d6be6479793e473fcfa4efe2c81b0b7b93b7..705a3b1a1b92c9c391d4c1794b968cbe48cdd096 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayClient.java @@ -1,80 +1,83 @@ -package MV3500Cohort2020JulySeptember.homework2.Garibay; - -import java.io.*; -import java.net.*; -/** - * - * @author Chris - */ -public class GaribayClient { - - // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 - public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; - - public static void main(String[] args) { - - // Local vars/fields - Socket socket; - InputStream is; - InputStreamReader isr; - BufferedReader br; - String serverMessage; - Integer count = 0; - OutputStream os; - PrintStream ps; - - - try { - while (true) { - System.out.println("TcpExample3Client 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 a Socket - // object; the server uses a ServerSocket to wait for - // connections. - socket = new Socket(LOCALHOST, 2317); // locohost? - - // Now hook everything up (i.e. set up the streams), Java style: - is = socket.getInputStream(); - isr = new InputStreamReader(is); - br = new BufferedReader(isr); - - - // Read a single line written by the server. We'd - // do things a bit differently if there were many lines to be read - // from the server instead of one only. - serverMessage = br.readLine(); - System.out.println("=================================================="); - System.out.println("'How many beer bottles are on the wall?"); - System.out.println("The GaribayServer responds by saying: '" + serverMessage + "'"); - System.out.println("GaribayClient says, 'There are "+ count +" beer bottles on the wall. Always good to have more beer bottles."); - count++; - // socket gets closed, either automatically/silently by this code (or possibly by the server) - - - // Now hook everything up (i.e. set up the streams), Java style: - os = socket.getOutputStream(); - ps = new PrintStream(os); - ps.println("GaribayClient message."); // this gets sent back to client! - - ps.flush(); - - - - } // end while(true) - } catch (IOException e) { - System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening - System.err.println("Error: " + e); - - // Provide more helpful information to user if exception occurs due to running twice at one time - if (e instanceof java.net.BindException) { - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } finally { - - // program exit: tell somebody about that - System.out.println("\nclient exit"); - } - } -} +package MV3500Cohort2020JulySeptember.homework2.Garibay; + +import java.io.*; +import java.net.*; +/** + * + * @author Chris + */ +public class GaribayClient { + + // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + /** socket value of shared interest */ + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) { + + // Local vars/fields + Socket socket; + InputStream is; + InputStreamReader isr; + BufferedReader br; + String serverMessage; + Integer count = 0; + OutputStream os; + PrintStream ps; + + + try { + while (true) { + System.out.println("TcpExample3Client 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 a Socket + // object; the server uses a ServerSocket to wait for + // connections. + socket = new Socket(LOCALHOST, 2317); // locohost? + + // Now hook everything up (i.e. set up the streams), Java style: + is = socket.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + serverMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("'How many beer bottles are on the wall?"); + System.out.println("The GaribayServer responds by saying: '" + serverMessage + "'"); + System.out.println("GaribayClient says, 'There are "+ count +" beer bottles on the wall. Always good to have more beer bottles."); + count++; + // socket gets closed, either automatically/silently by this code (or possibly by the server) + + + // Now hook everything up (i.e. set up the streams), Java style: + os = socket.getOutputStream(); + ps = new PrintStream(os); + ps.println("GaribayClient message."); // this gets sent back to client! + + ps.flush(); + + + + } // end while(true) + } catch (IOException e) { + System.err.println("Problem with TcpExample3ServerClient networking:"); // describe what is happening + System.err.println("Error: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } finally { + + // program exit: tell somebody about that + System.out.println("\nclient exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java index 353cdebcd1e38aeb2397d457b5a97ec064a4fc7c..7be36855fd8864ef7092baa394ce8e8e0a15fc20 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Garibay/GaribayServer.java @@ -1,93 +1,96 @@ -package MV3500Cohort2020JulySeptember.homework2.Garibay; - -import java.io.*; -import java.net.*; - -/** - * - * @author Chris - */ -public class GaribayServer { - - 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. - System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first - - ServerSocket serverSocket = new ServerSocket(2317); - OutputStream os; - PrintStream ps; - InetAddress localAddress, remoteAddress; - - InputStream is; - InputStreamReader isr; - BufferedReader br; - String clientMessage; - int localPort, remotePort; - - // Server is up and waiting (i.e. "blocked" or paused) - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. - while (true) { - - // block until connected to a client - try (Socket clientConnection = serverSocket.accept()) { - - // Now hook everything up (i.e. set up the streams), Java style: - os = clientConnection.getOutputStream(); - ps = new PrintStream(os); - ps.println("Count for yourself!"); // this gets sent back to client! - - // Print some information locally about the Socket connection. - // This includes the port and IP numbers on both sides (the socket pair). - localAddress = clientConnection.getLocalAddress(); - remoteAddress = clientConnection.getInetAddress(); - localPort = clientConnection.getLocalPort(); - 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("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " - + remoteAddress.toString() + ", " + remotePort + " ))"); - - // Notice the use of flush() and try w/ resources. Without - // the try w/ resources the Socket object may stay open for - // a while after the client has stopped needing this - // connection. try w/ resources explicitly ends the connection. - - // Now hook everything up (i.e. set up the streams), Java style: - is = clientConnection.getInputStream(); - isr = new InputStreamReader(is); - br = new BufferedReader(isr); - - - // Read a single line written by the server. We'd - // do things a bit differently if there were many lines to be read - // from the server instead of one only. - clientMessage = br.readLine(); - System.out.println("=================================================="); - System.out.println("Now we're talking!"); - System.out.println("The message the server sent was: '" + clientMessage + "'"); -// System.out.println("This was the "+ count +" connection."); - - - ps.flush(); - // like it or not, you're outta here! - } - } - } catch (IOException e) { - System.err.println("Problem with TcpExample3Server networking: " + e); - - // Provide more helpful information to user if exception occurs due to running twice at one time - if (e instanceof java.net.BindException) { - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } - } -} +package MV3500Cohort2020JulySeptember.homework2.Garibay; + +import java.io.*; +import java.net.*; + +/** + * + * @author Chris + */ +public class GaribayServer { + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + 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. + System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2317); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + + InputStream is; + InputStreamReader isr; + BufferedReader br; + String clientMessage; + int localPort, remotePort; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + + // block until connected to a client + try (Socket clientConnection = serverSocket.accept()) { + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnection.getOutputStream(); + ps = new PrintStream(os); + ps.println("Count for yourself!"); // this gets sent back to client! + + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnection.getLocalAddress(); + remoteAddress = clientConnection.getInetAddress(); + localPort = clientConnection.getLocalPort(); + 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("TcpExample3Server socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + // Notice the use of flush() and try w/ resources. Without + // the try w/ resources the Socket object may stay open for + // a while after the client has stopped needing this + // connection. try w/ resources explicitly ends the connection. + + // Now hook everything up (i.e. set up the streams), Java style: + is = clientConnection.getInputStream(); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); + + + // Read a single line written by the server. We'd + // do things a bit differently if there were many lines to be read + // from the server instead of one only. + clientMessage = br.readLine(); + System.out.println("=================================================="); + System.out.println("Now we're talking!"); + System.out.println("The message the server sent was: '" + clientMessage + "'"); +// System.out.println("This was the "+ count +" connection."); + + + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server networking: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java index 092a90ba4ddbd267e7aa0eccc98a4b70f25c3663..919c8b1ac05de1beb082669583020a97b5217f53 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Goericke/GoerickeClient.java @@ -15,9 +15,11 @@ import java.net.Socket; */ public class GoerickeClient { - // IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + /** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 */ public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { // Local vars/fields diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java index 98c79c0360cabe7614d5c22c371c5a7d0958df58..9391bb86c2817cfc830ecd0e4c6ae725c330512b 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java @@ -1,88 +1,90 @@ - -package MV3500Cohort2020JulySeptember.homework3.Garibay; - -import java.io.ByteArrayInputStream; -import java.io.DataInputStream; -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; - -/** - * - * @author chris - */ -public class UDPReceiverGaribay -{ -// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here - public static final int RECEIVING_PORT = 1415; - public static final String DESINATION_HOST = "localhost"; - - /** - * @param args command-line arguments - * @throws java.io.IOException - */ - public static void main(String[] args) throws IOException - { - DatagramSocket udpSocket = null; - - try - { - System.out.println(UDPReceiverGaribay.class.getName() + " started..."); - - // Create a UDP socket - udpSocket = new DatagramSocket(RECEIVING_PORT); - udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good - udpSocket.setBroadcast(false); // we're just receiving here - - byte[] byteArray = new byte[1500]; - DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length); - - ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); - DataInputStream dis = new DataInputStream(bais); - - boolean isEvenParity; - int packetCount = 0; - int firstInt; - float secondFloat; - String thirdString; - String padding; - - // You need a new receiving packet to read from every packet received - while (true) - { - packetCount++; // good practice to increment counter at start of loop, when possible - udpSocket.receive(receivePacket); // blocks until packet is received - - // values of interest follow. order and types of what was sent must match what you are reading! - firstInt = dis.readInt(); // packetID - secondFloat = dis.readFloat(); - thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit - isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for. - - dis.reset(); // now clear the input stream after reading, in preparation for next loop - - if (isEvenParity) - padding = " "; - else padding = ""; - - System.out.println("[" + UDPReceiverGaribay.class.getName() + "]" + - " packetID=" + firstInt + // have output message use same name as sender - ", second float value=" + secondFloat + - ", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character - " isPacketIdEvenParity=" + isEvenParity + "," + padding + - " packet counter=" + packetCount); - } - } - catch(IOException e) - { - System.err.println("Problem with UdpReceiver, see exception trace:"); - System.err.println(e); - } - finally // clean up prior to exit, don't want to leave behind zombie socket - { - if (udpSocket != null) - udpSocket.close(); - System.out.println(UDPReceiverGaribay.class.getName() + " complete."); // all done - } - } -} + +package MV3500Cohort2020JulySeptember.homework3.Garibay; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; + +/** + * + * @author chris + */ +public class UDPReceiverGaribay +{ +// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here + /** socket value of shared interest */ + public static final int RECEIVING_PORT = 1415; + /** socket value of shared interest */ + public static final String DESINATION_HOST = "localhost"; + + /** + * @param args command-line arguments + * @throws java.io.IOException + */ + public static void main(String[] args) throws IOException + { + DatagramSocket udpSocket = null; + + try + { + System.out.println(UDPReceiverGaribay.class.getName() + " started..."); + + // Create a UDP socket + udpSocket = new DatagramSocket(RECEIVING_PORT); + udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good + udpSocket.setBroadcast(false); // we're just receiving here + + byte[] byteArray = new byte[1500]; + DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length); + + ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); + DataInputStream dis = new DataInputStream(bais); + + boolean isEvenParity; + int packetCount = 0; + int firstInt; + float secondFloat; + String thirdString; + String padding; + + // You need a new receiving packet to read from every packet received + while (true) + { + packetCount++; // good practice to increment counter at start of loop, when possible + udpSocket.receive(receivePacket); // blocks until packet is received + + // values of interest follow. order and types of what was sent must match what you are reading! + firstInt = dis.readInt(); // packetID + secondFloat = dis.readFloat(); + thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit + isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for. + + dis.reset(); // now clear the input stream after reading, in preparation for next loop + + if (isEvenParity) + padding = " "; + else padding = ""; + + System.out.println("[" + UDPReceiverGaribay.class.getName() + "]" + + " packetID=" + firstInt + // have output message use same name as sender + ", second float value=" + secondFloat + + ", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character + " isPacketIdEvenParity=" + isEvenParity + "," + padding + + " packet counter=" + packetCount); + } + } + catch(IOException e) + { + System.err.println("Problem with UdpReceiver, see exception trace:"); + System.err.println(e); + } + finally // clean up prior to exit, don't want to leave behind zombie socket + { + if (udpSocket != null) + udpSocket.close(); + System.out.println(UDPReceiverGaribay.class.getName() + " complete."); // all done + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java index 8d6a81c06dd090f9d281f90818551cfed1c50a11..461af85871491b0b4fbd048b81a44256108d3741 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java @@ -1,112 +1,119 @@ - -package MV3500Cohort2020JulySeptember.homework3.Garibay; - -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; - -/** - * - * @author chris - */ -public class UDPSenderGaribay -{ - // System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html - public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) -// public static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port - public static final int RECEIVING_PORT = 1415; - public static final int TOTAL_PACKETS_TO_SEND = 100; - - // here is what we need for lab comms - // Bill Mahan's host ip - public static final String DESTINATION_HOST = "10.1.105.13"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever - - @SuppressWarnings("SleepWhileInLoop") - public static void main(String[] args) throws IOException - { - DatagramSocket udpSocket = null; - DataOutputStream dos = null; - int packetID = 0; // counter variable to send in packet - float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur - String message = MY_NAME + "It's not a trick question!"; // no really - String padding = new String(); - - try - { - System.out.println(UDPSenderGaribay.class.getName() + " shows how to send simple-type values via DataOutputStream"); - System.out.println(UDPSenderGaribay.class.getName() + " started..."); - - // Create a UDP socket - udpSocket = new DatagramSocket(); // let system assign output port, then SENDING_PORT not needed - - // Put together a message with binary content. "ByteArrayOutputStream" - // is a java.io utility that lets us put together an array of binary - // data, which we put into the UDP packet. - - ByteArrayOutputStream baos = new ByteArrayOutputStream(1500); // how many bytes are in buffer? MTU=1500 is good - dos = new DataOutputStream(baos); // wrapper for writing values, connects both streams - - // Put together a packet to send - // these types and order of variables must match on sender and receiver - byte[] byteArray = baos.toByteArray(); - - // ID of the host we are sending to - InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST); - // ID of the host we are sending from - InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified - - DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT); - - // Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does - // this cause network problems? (hint: yes for an unlimited send rate, unlike TCP). - // How do you know on the receiving side that you haven't received a - // duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility. - - for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill! - { - packetID++; // increment counter, prefer using explicit value to index - value = TOTAL_PACKETS_TO_SEND - packetID; // countdown - boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity - - // values of interest follow. order and types of what was sent must match what you are reading! - dos.writeInt (packetID); - dos.writeFloat (value); - dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit - dos.writeBoolean(isPacketIdEvenParity); - - dos.flush(); // sends DataOutputStream to ByteArrayOutputStream - byteArray = baos.toByteArray(); // OK so go get the flushed result... - datagramPacket.setData(byteArray); // and put it in the packet... - udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking. -// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system - - if (isPacketIdEvenParity) - padding = " "; - else padding = ""; - - Thread.sleep(1000); // Send packets at rate of one per second - System.out.println("[" + UDPSenderGaribay.class.getName() + "] " + MY_NAME + " " + sourceAddress + - " sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity + - ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND); - baos.reset(); // clear the output stream after sending - } - } - catch (IOException | InterruptedException e) - { - System.err.println("Problem with UDPSenderGaribay, see exception trace:"); - System.err.println(e); - } - finally // clean up prior to exit, don't want to leave behind zombies - { - if (udpSocket != null) - udpSocket.close(); - - if (dos != null) - dos.close(); - System.out.println(UDPSenderGaribay.class.getName() + " complete."); // all done - } - } -} + +package MV3500Cohort2020JulySeptember.homework3.Garibay; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; + +/** + * + * @author chris + */ +public class UDPSenderGaribay +{ + // System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html + public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) +// public static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port + public static final int RECEIVING_PORT = 1415; + public static final int TOTAL_PACKETS_TO_SEND = 100; + + /** here is what we need for lab comms; Bill Mahan's host ip */ + public static final String DESTINATION_HOST = "10.1.105.13"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + @SuppressWarnings("SleepWhileInLoop") + public static void main(String[] args) + { + DatagramSocket udpSocket = null; + DataOutputStream dos = null; + int packetID = 0; // counter variable to send in packet + float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur + String message = MY_NAME + "It's not a trick question!"; // no really + String padding = new String(); + + try + { + System.out.println(UDPSenderGaribay.class.getName() + " shows how to send simple-type values via DataOutputStream"); + System.out.println(UDPSenderGaribay.class.getName() + " started..."); + + // Create a UDP socket + udpSocket = new DatagramSocket(); // let system assign output port, then SENDING_PORT not needed + + // Put together a message with binary content. "ByteArrayOutputStream" + // is a java.io utility that lets us put together an array of binary + // data, which we put into the UDP packet. + + ByteArrayOutputStream baos = new ByteArrayOutputStream(1500); // how many bytes are in buffer? MTU=1500 is good + dos = new DataOutputStream(baos); // wrapper for writing values, connects both streams + + // Put together a packet to send + // these types and order of variables must match on sender and receiver + byte[] byteArray = baos.toByteArray(); + + // ID of the host we are sending to + InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST); + // ID of the host we are sending from + InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified + + DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT); + + // Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does + // this cause network problems? (hint: yes for an unlimited send rate, unlike TCP). + // How do you know on the receiving side that you haven't received a + // duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility. + + for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill! + { + packetID++; // increment counter, prefer using explicit value to index + value = TOTAL_PACKETS_TO_SEND - packetID; // countdown + boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity + + // values of interest follow. order and types of what was sent must match what you are reading! + dos.writeInt (packetID); + dos.writeFloat (value); + dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit + dos.writeBoolean(isPacketIdEvenParity); + + dos.flush(); // sends DataOutputStream to ByteArrayOutputStream + byteArray = baos.toByteArray(); // OK so go get the flushed result... + datagramPacket.setData(byteArray); // and put it in the packet... + udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking. +// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system + + if (isPacketIdEvenParity) + padding = " "; + else padding = ""; + + Thread.sleep(1000); // Send packets at rate of one per second + System.out.println("[" + UDPSenderGaribay.class.getName() + "] " + MY_NAME + " " + sourceAddress + + " sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity + + ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND); + baos.reset(); // clear the output stream after sending + } + } + catch (IOException | InterruptedException e) + { + System.err.println("Problem with UDPSenderGaribay, see exception trace:"); + System.err.println(e); + } + finally // clean up prior to exit, don't want to leave behind zombies + { + if (udpSocket != null) + udpSocket.close(); + try { + if (dos != null) + dos.close(); + } + catch (IOException e) + { + System.err.println("Problem with UDPSenderGaribay, see exception trace:"); + System.err.println(e); + } + System.out.println(UDPSenderGaribay.class.getName() + " complete."); // all done + } + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Goericke/TCPNumberReceiverUDPResultSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Goericke/TCPNumberReceiverUDPResultSender.java index 561f3537470dcac8fadeb17873a7b3034bad2dc0..010f6e9c62e1821c45058248adbe87f0fb9089e5 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Goericke/TCPNumberReceiverUDPResultSender.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Goericke/TCPNumberReceiverUDPResultSender.java @@ -21,7 +21,7 @@ import java.net.Socket; */ public class TCPNumberReceiverUDPResultSender { - // Change this to the port where the TCP server is listening + /** Change this to the port where the TCP server is listening */ public static final int TCP_ARGON_SERVER_PORT = 2317; /**