diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastReceiver.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastReceiver.java index 7fdaf94ecca0a3578b9ca3bdda4727bb908288a6..407c3c3ab8f6db37940a3d7f98ff35c3fec1da55 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastReceiver.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastReceiver.java @@ -12,11 +12,15 @@ import java.net.*; */ public class AngelMulticastReceiver { + /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastSenderExample.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastSenderExample.java index bcb08fbe28d9248d8a4e6825353f30a6097ec042..36da85088b2648f5a09b23e8266013fdd874e7b7 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastSenderExample.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/AngelMulticastSenderExample.java @@ -14,11 +14,15 @@ import java.util.Random; */ public class AngelMulticastSenderExample { + /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastReceiver.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastReceiver.java index d7e9a8945f00a0b9eae2aaa9f842fb657245d5c8..9ad31b8bfc414d5bf7e862e08461dfe46e5c69c8 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastReceiver.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastReceiver.java @@ -1,73 +1,77 @@ package MV3500Cohort2018JanuaryMarch.homework2; -/* - * 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. - */ - - -import java.io.ByteArrayInputStream; -import java.io.DataInputStream; -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.MulticastSocket; - -/** - * - * @author Michael - */ -public class BlankenbekerMulticastReceiver { - - public static final String MULTICAST_ADDRESS = "239.1.2.15"; - public static final int DESTINATION_PORT = 1717; - /** How many routers can be crossed */ - public static final int TTL = 10; - - public static void main(String[] args) - { - try - { - - // This is a java/IPv6 problem. You should also add it to the - // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true - // set in the "run" section of preferences. Also, typically - // netbeans must be restarted after these settings. - // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache - System.setProperty("java.net.preferIPv4Stack", "true"); - - - MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); - multicastSocket.setTimeToLive(TTL); - InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); - System.out.println(multicastAddress); - // Join group useful on receiving side - multicastSocket.joinGroup(multicastAddress); - // You can join multiple groups here - - int count = 0; - - while(true) - { - byte[] packetArray = new byte[1500]; - DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); - - multicastSocket.receive(packet); - count++; - - ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); - DataInputStream dis = new DataInputStream(bais); - float firstNumber = dis.readFloat(); - float secondNumber = dis.readFloat(); - float thirdNumber = dis.readFloat(); - - System.out.println("Number received: " + count + " Vehicle position is: (" + firstNumber + ", " + secondNumber+", "+thirdNumber+")"); - } - } - catch(Exception e) - { - System.out.println(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. + */ + + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; + +/** + * + * @author Michael + */ +public class BlankenbekerMulticastReceiver { + + /** socket value of shared interest */ + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ + public static final int DESTINATION_PORT = 1717; + /** How many routers can be crossed */ + public static final int TTL = 10; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + + MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + + int count = 0; + + while(true) + { + byte[] packetArray = new byte[1500]; + DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); + + multicastSocket.receive(packet); + count++; + + ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); + DataInputStream dis = new DataInputStream(bais); + float firstNumber = dis.readFloat(); + float secondNumber = dis.readFloat(); + float thirdNumber = dis.readFloat(); + + System.out.println("Number received: " + count + " Vehicle position is: (" + firstNumber + ", " + secondNumber+", "+thirdNumber+")"); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastSender.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastSender.java index c19bf55e3f92ca174c237408a716062436587c0d..9b2ad6e5bd56d925753b298efc510cd366271713 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastSender.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerMulticastSender.java @@ -1,94 +1,98 @@ package MV3500Cohort2018JanuaryMarch.homework2; -/* - * 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. - */ - - -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.MulticastSocket; - -/** - * - * @author Michael - */ -public class BlankenbekerMulticastSender { - - public static final String MULTICAST_ADDRESS = "239.1.2.15"; - public static final int DESTINATION_PORT = 1717; - /** How many routers can be crossed */ - public static final int TTL = 10; - - public static void main(String[] args) - { - BlankenbekerTruck truck = new BlankenbekerTruck(); - while (true) { - truck.move(); - try - { - // This is a java/IPv6 problem. You should also add it to the - // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true - // set in the "run" section of preferences. Also, typically - // netbeans must be restarted after these settings. - // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache - System.setProperty("java.net.preferIPv4Stack", "true"); - - - - MulticastSocket multicastSocket = new MulticastSocket(1718); - multicastSocket.setTimeToLive(TTL); - InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); - System.out.println(multicastAddress); - // Join group useful on receiving side - multicastSocket.joinGroup(multicastAddress); - // You can join multiple groups here - - // 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(); - DataOutputStream dos = new DataOutputStream(baos); - float x = truck.getX(); - float y = truck.getY(); - float z = truck.getZ(); - - dos.writeFloat(x); - dos.writeFloat(y); - dos.writeFloat(z); - - byte[] buffer = baos.toByteArray(); - - // Put together a packet to send - - // muticast group we are sending to--not a single host - - DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); - - // How fast does this go? Does UDP try to slow it 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? - - for(int idx = 0; idx < 1; idx++) - { - - multicastSocket.send(packet); - Thread.sleep(1000); // Send 100, one per second - System.out.println("Sent new multicast packet 1 of 1"); - } - } - catch(Exception e) - { - System.out.println(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. + */ + + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; + +/** + * + * @author Michael + */ +public class BlankenbekerMulticastSender { + + /** socket value of shared interest */ + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ + public static final int DESTINATION_PORT = 1717; + /** How many routers can be crossed */ + public static final int TTL = 10; + + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + BlankenbekerTruck truck = new BlankenbekerTruck(); + while (true) { + truck.move(); + try + { + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + + + MulticastSocket multicastSocket = new MulticastSocket(1718); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + + // 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(); + DataOutputStream dos = new DataOutputStream(baos); + float x = truck.getX(); + float y = truck.getY(); + float z = truck.getZ(); + + dos.writeFloat(x); + dos.writeFloat(y); + dos.writeFloat(z); + + byte[] buffer = baos.toByteArray(); + + // Put together a packet to send + + // muticast group we are sending to--not a single host + + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + + // How fast does this go? Does UDP try to slow it 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? + + for(int idx = 0; idx < 1; idx++) + { + + multicastSocket.send(packet); + Thread.sleep(1000); // Send 100, one per second + System.out.println("Sent new multicast packet 1 of 1"); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerTruck.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerTruck.java index 59f0a08ba19334a70651694fb626a5924716d1e6..3b4d93dfb28b2ee7e30fac07049466838340d1e4 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerTruck.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/BlankenbekerTruck.java @@ -1,46 +1,54 @@ package MV3500Cohort2018JanuaryMarch.homework2; -/* - * 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; - -/** - * - * @author Michael - */ -public class BlankenbekerTruck { - private float x; - private float y; - private float z; - - public BlankenbekerTruck(){ - x = 0; - y=0; - z=0; - } - - public void move(){ - x+=1.5; - y+=1.2; - z-=.2; -} - /** - * - * @return - */ - public float getX(){ - return x; - } - - public float getY(){ - return y; - } - - public float getZ(){ - return z; - } -} - +/* + * 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; + +/** + * + * @author Michael + */ +public class BlankenbekerTruck { + private float x; + private float y; + private float z; + + public BlankenbekerTruck(){ + x = 0; + y=0; + z=0; + } + + public void move(){ + x+=1.5; + y+=1.2; + z-=.2; +} + /** + * + * @return X value + */ + public float getX(){ + return x; + } + + /** + * + * @return Y value + */ + public float getY(){ + return y; + } + + /** + * + * @return Z value + */ + public float getZ(){ + return z; + } +} + diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastReceiver.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastReceiver.java index e02345c08f84d0be8d352c90be4dc2e019b34f06..d2be90d6c439879579a0fd74c773fc63f7535be3 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastReceiver.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastReceiver.java @@ -12,12 +12,20 @@ import java.net.UnknownHostException; * * @author emilyconard */ -public class ConardMulticastReceiver { +public class ConardMulticastReceiver +{ + /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; + + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastSender.java b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastSender.java index 108b25449ed8af73af3ed61fe47b51f7f6eb839b..2e81bb6d70b456109ccf93111c670950860d5fa5 100644 --- a/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastSender.java +++ b/assignments/src/MV3500Cohort2018JanuaryMarch/homework2/ConardMulticastSender.java @@ -15,11 +15,17 @@ import java.net.MulticastSocket; */ public class ConardMulticastSender { + /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java index 2ff1197d588b0ddbf8c023dec23515fc942803e5..22db49e1482ee7b9a2f60fc708e47447aac97f67 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/DemchkoAssignment2.java @@ -8,7 +8,7 @@ public class DemchkoAssignment2 { /** run the program * @param args command-line arguments, string parameters (unused) */ - public static void main(String[] args) + public static void main(String[] args) { try { diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java index e7539db6d46db46600194048c6c5a128a73e153f..d6ea71015a827b2c2947a8add8a71bb6de3beb8a 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework1/FurrAssignment2.java @@ -33,7 +33,7 @@ public class FurrAssignment2 { /** run the program * @param args command-line arguments, string parameters (unused) */ - public static void main(String[] args) throws IOException + public static void main(String[] args) { try { diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Demchko/DemchkoClient.java b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Demchko/DemchkoClient.java index ddd5065c452df26c7db11b83dbcd9855fad46e3a..e8f09c2f90c8281d9c835cd71a0b1b82f6b0563d 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Demchko/DemchkoClient.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Demchko/DemchkoClient.java @@ -1,81 +1,82 @@ -/* - * 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 MV3500Cohort2018JulySeptember.homework2.Demchko; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintStream; -import java.net.Socket; -import java.util.Scanner; - -/** - * - * @author ekdem - */ -public class DemchkoClient { - - public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 - - /** - * Program invocation, execution starts here - * @param args command-line arguments - */ - public static void main(String[] args) { - - try { - System.out.println("Answer this question:"); - - //This program establishes allows a client and server - //to send messages back and forth. - System.out.println("Why did the chicken cross the road?"); - while (true) { - // 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); // this is the local host for the socket - // 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. - OutputStream os = socket.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - BufferedWriter bw = new BufferedWriter(osw); - - InputStream is = socket.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - - String inputMessage = "To get to the other side"; - //System.out.println("Joe says: Who's there? "); - String sendMessage = inputMessage + "\n"; - bw.write(sendMessage); - bw.flush(); - String input = br.readLine(); - System.out.println(input + "\n" + input + " no."); - - // String serverMessage = br.readLine(); - System.out.println("Try again." ); // + serverMessage - - String update = br.readLine(); - // String sendUpdate = update + "\n"; - System.out.println("The correct answer is to get to the other side"); - - } // end while(true) - } catch (IOException e) { - System.out.println("Houston we have a problem: "); // describe what is happening - System.out.println(e); - } - // program exit - System.out.println("Demchko, out"); - } - -} +/* + * 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 MV3500Cohort2018JulySeptember.homework2.Demchko; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.net.Socket; +import java.util.Scanner; + +/** + * + * @author ekdem + */ +public class DemchkoClient +{ + /** socket value of shared interest */ + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String[] args) { + + try { + System.out.println("Answer this question:"); + + //This program establishes allows a client and server + //to send messages back and forth. + System.out.println("Why did the chicken cross the road?"); + while (true) { + // 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); // this is the local host for the socket + // 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. + OutputStream os = socket.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + BufferedWriter bw = new BufferedWriter(osw); + + InputStream is = socket.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String inputMessage = "To get to the other side"; + //System.out.println("Joe says: Who's there? "); + String sendMessage = inputMessage + "\n"; + bw.write(sendMessage); + bw.flush(); + String input = br.readLine(); + System.out.println(input + "\n" + input + " no."); + + // String serverMessage = br.readLine(); + System.out.println("Try again." ); // + serverMessage + + String update = br.readLine(); + // String sendUpdate = update + "\n"; + System.out.println("The correct answer is to get to the other side"); + + } // end while(true) + } catch (IOException e) { + System.out.println("Houston we have a problem: "); // describe what is happening + System.out.println(e); + } + // program exit + System.out.println("Demchko, out"); + } + +} diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java index be7f2ab9e18d7f7a9590ab0089ba6575cd5b4ef8..936249208f5d43df9f77fd6c2a2987592a6b640f 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework2/Frisco/FriscoAssignment2Client.java @@ -17,6 +17,7 @@ import java.util.Scanner; public class FriscoAssignment2Client { + /** socket value of shared interest */ public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 /** diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Receiver.java b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Receiver.java index 745989fe9dd76f2bd8cf556990aa183872a9ea33..1d6625175de65938f213b32df109ff300425559f 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Receiver.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Receiver.java @@ -15,9 +15,11 @@ import java.net.MulticastSocket; * * @author danielcain with credit to CDR Angelopolis */ -public class CainThomersonHw3Receiver { - +public class CainThomersonHw3Receiver +{ + /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; diff --git a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Sender.java b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Sender.java index 72410747d62e0530061faca73e25cd966bce9c7d..3055d1e598465be74e92acf02770712bcc0b875f 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Sender.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/homework3/Cain_Thomerson_Homework3/CainThomersonHw3Sender.java @@ -4,6 +4,7 @@ package MV3500Cohort2018JulySeptember.homework3.Cain_Thomerson_Homework3; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; +import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; @@ -14,9 +15,11 @@ import java.util.Random; * * @author danielcain with credit to CDR Angelopolis */ -public class CainThomersonHw3Sender { - -public static final String MULTICAST_ADDRESS = "239.1.2.15"; +public class CainThomersonHw3Sender +{ + /** socket value of shared interest */ + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; @@ -31,8 +34,7 @@ public static final String MULTICAST_ADDRESS = "239.1.2.15"; { System.setProperty("java.net.preferIPv4Stack", "true"); - - + MulticastSocket multicastSocket = new MulticastSocket(1718); multicastSocket.setTimeToLive(TTL); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); @@ -71,9 +73,9 @@ public static final String MULTICAST_ADDRESS = "239.1.2.15"; byte[] buffer = byteOut.toByteArray(); // Put together a packet to send - // muticast group we are sending to--not a single host + // multicast group we are sending to--not a single host - DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); // How fast does this go? Does UDP try to slow it down, or does // this cause network problems? (hint: yes for an unlimited send @@ -81,14 +83,13 @@ public static final String MULTICAST_ADDRESS = "239.1.2.15"; // that you haven't received a duplicate UDP packet, out of // order packet, or dropped packet? - - multicastSocket.send(packet); + multicastSocket.send(packet); byteOut.reset(); Thread.sleep(1000); // Send 100, one per second System.out.println("Sent multicast packet " + idx + " of 100"); } } - catch(Exception e) + catch(IOException | InterruptedException e) { System.out.println(e); } diff --git a/assignments/src/MV3500Cohort2018JulySeptember/projects/CainThomersonFinal/C_T_EspduRequestingUnit.java b/assignments/src/MV3500Cohort2018JulySeptember/projects/CainThomersonFinal/C_T_EspduRequestingUnit.java index 6a6cc0f0a14261378e2665df6fd552f08a6a207f..e78ee0eddeb1be3c9c424e6a8647565656c8e863 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/projects/CainThomersonFinal/C_T_EspduRequestingUnit.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/projects/CainThomersonFinal/C_T_EspduRequestingUnit.java @@ -17,7 +17,7 @@ import edu.nps.moves.disutil.PduFactory; */ public class C_T_EspduRequestingUnit { - public static final int NUMBER_TO_SEND = 5000; + private static final int NUMBER_TO_SEND = 5000; /** Type of network connection */ public enum NetworkMode { @@ -29,10 +29,13 @@ public class C_T_EspduRequestingUnit { BROADCAST }; + /** socket value of shared interest */ public static final int MAX_PDU_SIZE = 8192; + /** socket value of shared interest */ public static final String DEFAULT_MULTICAST_ADDRESS = "239.1.2.3"; + /** socket value of shared interest */ public static final int DEFAULT_MULTICAST_PORT = 3000; /** diff --git a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/FDCSendRecieve.java b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/FDCSendRecieve.java index 6a2ad25b9488ca7105738ec578c99c976da97dc4..79ceb9f95f6afb56c061037560a5989db36af0ca 100644 --- a/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/FDCSendRecieve.java +++ b/assignments/src/MV3500Cohort2018JulySeptember/projects/FriscoFurr/FDCSendRecieve.java @@ -1,296 +1,298 @@ -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 FDCSendRecieve { - - /** - * 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(); - - public FDCSendRecieve(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; - System.out.println("\nFDC Sender started..."); - // 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 sender, recived PDU type: " + currentPduType); - - switch (currentPduType) // using enumeration values from edu.nps.moves.disenum.* - { - - case 1: //ENTITY_STATE: - aPdu = pdu; - break; - - case 2: //FIRE - aPdu = pdu; - break; -// - case 15: //AcknowledgePdu - aPdu = pdu; - break; - - case 17: - aPdu = pdu; - break; - - case 14: - aPdu = pdu; - break; - - default: - System.out.print("PDU of type " + pdu + " not supported, 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("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 130 (2nd statement in the main function) - * @param args command-line arguments - * @throws IOException input-output error - * @throws InterruptedException interruption - */ - public static void main(String[] args) throws IOException, InterruptedException { - DisTime disTime = DisTime.getInstance(); - - FDCSendRecieve sender = new FDCSendRecieve(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); //initalize the sender - - EntityStatePdu FDCespdu = new EntityStatePdu(); - Marking marking = new Marking(); - marking.setCharactersString("FDC"); - FDCespdu.setMarking(marking); - FDCespdu.setExerciseID((short) 1); - EntityID FDCID = FDCespdu.getEntityID(); - FDCID.setSite(1); - FDCID.setApplication(1); - FDCID.setEntity(1); - EntityType entityType = FDCespdu.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) 3); // 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(0.0); //TGT = 150 OBS = 75 - location.setY(0.0); //TGT = 150 OBS = 75 - location.setZ(10.0); //TGT = 20 OBS = 50 - FDCespdu.setEntityLocation(location); - int timestamp = disTime.getDisAbsoluteTimestamp(); - FDCespdu.setTimestamp(timestamp); - - sender.run(FDCespdu); //sends inital here I am and who I am - - //Set up other players to look for: - EntityID OBSEntityID = new EntityID(); - OBSEntityID.setEntity(2); - OBSEntityID.setApplication(1); - OBSEntityID.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 = 0; //will use the curentPduType as the check for sending other packets. - - try { - System.out.println("FDC is alive and ready to recieve fire missions...\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; - } -// pdu.setExerciseID((short)5); - 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() == 225) { - Thread.sleep((long) 200); - timestamp = disTime.getDisAbsoluteTimestamp(); - FDCespdu.setTimestamp(timestamp); - System.out.println("Talking to the Observer, sending a radio check "); - sender.run(FDCespdu); - } - } - if (currentPduType == 16) //Action request - { - // Action response is sending a Null PDU, not sure why... so we will use the acknowledge pdu instead - AcknowledgePdu ack = new AcknowledgePdu(); - ack.setExerciseID((short) 1); - ack.setRequestID((long) 1); - timestamp = disTime.getDisAbsoluteTimestamp(); - ack.setTimestamp(timestamp); - - //Creating a fire PDU - FirePdu fire = new FirePdu(); - fire.setExerciseID((short) 1); - fire.setFireMissionIndex(1000); - fire.setRangeToTarget((float) Math.sqrt(Math.pow(150, 2) + Math.pow(150, 2))); //would pass in target info, but here we know location of tgt is (150,150) and FDC (0,0) - fire.setFiringEntityID(FDCID); - fire.setTargetEntityID(TGTEntityID); - timestamp = disTime.getDisAbsoluteTimestamp(); - fire.setTimestamp(timestamp); - sender.run(ack, fire); - } - - if (currentPduType == 22) //Comment PDU - { - AcknowledgePdu ack = new AcknowledgePdu(); - ack.setExerciseID((short) 1); - ack.setRequestID((long) 1); - timestamp = disTime.getDisAbsoluteTimestamp(); - ack.setTimestamp(timestamp); - - //and the freeze PDU being created to stop everything. - StopFreezePdu stop = new StopFreezePdu(); - stop.setExerciseID((short) 1); - stop.setRequestID((long) 1); - sender.run(ack, stop); - } - - } else { - System.out.println("received packet but pdu is null and originated from FDC. Still standing by..."); - } - } - } - } catch (IOException e) { - System.out.println("Problem with FDCSendRecieve, see exception trace:"); - System.out.println(e); - } finally { - System.out.println("FDC SendReceive 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 FDCSendRecieve { + + /** + * 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(); + + public FDCSendRecieve(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; + System.out.println("\nFDC Sender started..."); + // 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 sender, recived PDU type: " + currentPduType); + + switch (currentPduType) // using enumeration values from edu.nps.moves.disenum.* + { + + case 1: //ENTITY_STATE: + aPdu = pdu; + break; + + case 2: //FIRE + aPdu = pdu; + break; +// + case 15: //AcknowledgePdu + aPdu = pdu; + break; + + case 17: + aPdu = pdu; + break; + + case 14: + aPdu = pdu; + break; + + default: + System.out.print("PDU of type " + pdu + " not supported, 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("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 130 (2nd statement in the main function) + * @param args command-line arguments + * @throws IOException input-output error + * @throws InterruptedException interruption + */ + public static void main(String[] args) throws IOException, InterruptedException { + DisTime disTime = DisTime.getInstance(); + + FDCSendRecieve sender = new FDCSendRecieve(DEFAULT_MULTICAST_PORT, DEFAULT_MULTICAST_ADDRESS); //initalize the sender + + EntityStatePdu FDCespdu = new EntityStatePdu(); + Marking marking = new Marking(); + marking.setCharactersString("FDC"); + FDCespdu.setMarking(marking); + FDCespdu.setExerciseID((short) 1); + EntityID FDCID = FDCespdu.getEntityID(); + FDCID.setSite(1); + FDCID.setApplication(1); + FDCID.setEntity(1); + EntityType entityType = FDCespdu.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) 3); // 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(0.0); //TGT = 150 OBS = 75 + location.setY(0.0); //TGT = 150 OBS = 75 + location.setZ(10.0); //TGT = 20 OBS = 50 + FDCespdu.setEntityLocation(location); + int timestamp = disTime.getDisAbsoluteTimestamp(); + FDCespdu.setTimestamp(timestamp); + + sender.run(FDCespdu); //sends inital here I am and who I am + + //Set up other players to look for: + EntityID OBSEntityID = new EntityID(); + OBSEntityID.setEntity(2); + OBSEntityID.setApplication(1); + OBSEntityID.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 = 0; //will use the curentPduType as the check for sending other packets. + + try { + System.out.println("FDC is alive and ready to recieve fire missions...\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; + } +// pdu.setExerciseID((short)5); + 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() == 225) { + Thread.sleep((long) 200); + timestamp = disTime.getDisAbsoluteTimestamp(); + FDCespdu.setTimestamp(timestamp); + System.out.println("Talking to the Observer, sending a radio check "); + sender.run(FDCespdu); + } + } + if (currentPduType == 16) //Action request + { + // Action response is sending a Null PDU, not sure why... so we will use the acknowledge pdu instead + AcknowledgePdu ack = new AcknowledgePdu(); + ack.setExerciseID((short) 1); + ack.setRequestID((long) 1); + timestamp = disTime.getDisAbsoluteTimestamp(); + ack.setTimestamp(timestamp); + + //Creating a fire PDU + FirePdu fire = new FirePdu(); + fire.setExerciseID((short) 1); + fire.setFireMissionIndex(1000); + fire.setRangeToTarget((float) Math.sqrt(Math.pow(150, 2) + Math.pow(150, 2))); //would pass in target info, but here we know location of tgt is (150,150) and FDC (0,0) + fire.setFiringEntityID(FDCID); + fire.setTargetEntityID(TGTEntityID); + timestamp = disTime.getDisAbsoluteTimestamp(); + fire.setTimestamp(timestamp); + sender.run(ack, fire); + } + + if (currentPduType == 22) //Comment PDU + { + AcknowledgePdu ack = new AcknowledgePdu(); + ack.setExerciseID((short) 1); + ack.setRequestID((long) 1); + timestamp = disTime.getDisAbsoluteTimestamp(); + ack.setTimestamp(timestamp); + + //and the freeze PDU being created to stop everything. + StopFreezePdu stop = new StopFreezePdu(); + stop.setExerciseID((short) 1); + stop.setRequestID((long) 1); + sender.run(ack, stop); + } + + } else { + System.out.println("received packet but pdu is null and originated from FDC. Still standing by..."); + } + } + } + } catch (IOException e) { + System.out.println("Problem with FDCSendRecieve, see exception trace:"); + System.out.println(e); + } finally { + System.out.println("FDC SendReceive complete. - OUT!"); + } + + } + +} diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework1/McCannTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/McCannTcpExample1Telnet.java index b86e253e25fdfc3ff3b19378f0957888de4fbf00..1aed07bfb90e953036789ea127f2fe4cc03e4ecd 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/McCannTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/McCannTcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class McCannTcpExample1Telnet { + /** 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/SchuttTcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/SchuttTcpExample1Telnet.java index 9b3dfe7e9188f455462e2239cf021ed3962d7b33..12d3b55f88ba2de11683c27270aab4d987658424 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/SchuttTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/SchuttTcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class SchuttTcpExample1Telnet { + /** 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/YURKOVICH_TcpExample1Telnet.java b/assignments/src/MV3500Cohort2019JulySeptember/homework1/YURKOVICH_TcpExample1Telnet.java index ed463f87fc82c8efccee0d8e713455e6ce230dd3..077a09e121bf1575811d393fa5519fbbb084c788 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework1/YURKOVICH_TcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework1/YURKOVICH_TcpExample1Telnet.java @@ -31,6 +31,8 @@ import java.net.*; */ public class YURKOVICH_TcpExample1Telnet { + /** run the program + * @param args command-line arguments, string parameters (unused) */ public static void main(String[] args) { try diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework2/Fetterolf/FetterolfHomework2Client.java b/assignments/src/MV3500Cohort2019JulySeptember/homework2/Fetterolf/FetterolfHomework2Client.java index 53f59abbe6ab928ebea2421ef120773fdeb20d43..8667124650d52243ad5bd2674440aee79e461ea2 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework2/Fetterolf/FetterolfHomework2Client.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework2/Fetterolf/FetterolfHomework2Client.java @@ -14,8 +14,9 @@ import java.net.*; * * @author mcgredo */ -public class FetterolfHomework2Client { - +public class FetterolfHomework2Client +{ + /** socket value of shared interest */ public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; /** diff --git a/assignments/src/MV3500Cohort2019JulySeptember/homework4/Fetterolf/FetterolfPduReceiver.java b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Fetterolf/FetterolfPduReceiver.java index b5c3ef49b587bd15fefc995963f5a172739cac15..7dc61d603031c38a7ed40ce2df24b65a2ffd0722 100644 --- a/assignments/src/MV3500Cohort2019JulySeptember/homework4/Fetterolf/FetterolfPduReceiver.java +++ b/assignments/src/MV3500Cohort2019JulySeptember/homework4/Fetterolf/FetterolfPduReceiver.java @@ -10,9 +10,11 @@ import java.util.ArrayList; public class FetterolfPduReceiver { + /** socket value of shared interest */ public static final int DEFAULT_MULTICAST_PORT = FetterolfPduReceiver.DEFAULT_MULTICAST_PORT; + /** socket value of shared interest */ public static final String DEFAULT_MULTICAST_ADDRESS = FetterolfPduReceiver.DEFAULT_MULTICAST_ADDRESS; - public static final boolean USE_FAST_ESPDU = false; + private static final boolean USE_FAST_ESPDU = false; /** * Program invocation, execution starts here diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework1/GaribayTCPExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GaribayTCPExample1Telnet.java index d7058f112d68488dfc1af58db3fab665c93a98bc..7f915e744d25a487c3c91a59fb38d53100938e9f 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework1/GaribayTCPExample1Telnet.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GaribayTCPExample1Telnet.java @@ -1,64 +1,67 @@ - -package MV3500Cohort2020JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author chris - */ - - -public class GaribayTCPExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was written by Chris' server TcpExample1."); // to remote clientnc - ps.println("Fall forward. Every failed experiment is one step closer to success."); - System.out.println("Server response was written by Chris' server TcpExample1."); // to server console - System.out.println("Denzel Washington"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} - + +package MV3500Cohort2020JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author chris + */ + + +public class GaribayTCPExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was written by Chris' server TcpExample1."); // to remote clientnc + ps.println("Fall forward. Every failed experiment is one step closer to success."); + System.out.println("Server response was written by Chris' server TcpExample1."); // to server console + System.out.println("Denzel Washington"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/homework1/GoerickeTcpExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java index 009f541410e1619b42caeada953a47a54de95cc8..93931a9845fa6d4b58f2f2e34d1a8d7e7c7a3577 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/GoerickeTcpExample1Telnet.java @@ -1,72 +1,72 @@ - -package MV3500Cohort2020JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * Homework 1 for class MV3500 - Summer 2020 - * @author Goericke, Stefan - */ -public class GoerickeTcpExample1Telnet { - - /** - * @param args command-line arguments - */ - public static void main(String[] args) throws IOException { - System.out.println("Homework 1 MV3500 - Summer 2020 (Author: Stefan Goericke)"); - System.out.println("---------------------------------------------------------"); - String masterString = " ######### ###### # #\r\n"; - masterString = masterString + " # # # #\r\n"; - masterString = masterString + " # ###### # #\r\n"; - masterString = masterString + " # # # #\r\n"; - masterString = masterString + " # ###### #\r\n"; - - //build up connection - try - { - // build serverSOcket with port 2317 - ServerSocket serverSocket = new ServerSocket(2317); - System.out.println("ServerSocket created with port 2317 ..."); - - // open client connection. Wait for answer (method accept) - System.out.println("Open telnet client in cmd-console: telnet localhost 2317"); - Socket clientConnection = serverSocket.accept(); - System.out.println("Client Connection established ..."); - - // create stream-objects - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); // already declared outside try-block - System.out.println("Stream objects created ..."); - - try{// connection was established - System.out.println("Connection established, sending message --> /n"); - - ps.println("This client response was written by server TcpExample1."); - ps.println(masterString); - - System.out.println("This server response was written by server TcpExample1."); - System.out.println(masterString); - - ps.flush(); - } - catch(Exception e){ // in case message could not be send - System.out.println("Error while sending message .... press ENTER"); - System.in.read(); - System.exit(0); - - } - clientConnection.close(); - } - catch (IOException e){ // connection could not be established - System.out.println("Connection could not be established. Quit programm"); - System.in.read(); - System.exit(0); - } - - } - -} + +package MV3500Cohort2020JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Homework 1 for class MV3500 - Summer 2020 + * @author Goericke, Stefan + */ +public class GoerickeTcpExample1Telnet { + + /** + * @param args command-line arguments + */ + public static void main(String[] args) + { + System.out.println("Homework 1 MV3500 - Summer 2020 (Author: Stefan Goericke)"); + System.out.println("---------------------------------------------------------"); + String masterString = " ######### ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### # #\r\n"; + masterString = masterString + " # # # #\r\n"; + masterString = masterString + " # ###### #\r\n"; + + //build up connection + try + { + // build serverSOcket with port 2317 + ServerSocket serverSocket = new ServerSocket(2317); + System.out.println("ServerSocket created with port 2317 ..."); + + // open client connection. Wait for answer (method accept) + System.out.println("Open telnet client in cmd-console: telnet localhost 2317"); + Socket clientConnection = serverSocket.accept(); + System.out.println("Client Connection established ..."); + + // create stream-objects + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); // already declared outside try-block + System.out.println("Stream objects created ..."); + + try{// connection was established + System.out.println("Connection established, sending message --> /n"); + + ps.println("This client response was written by server TcpExample1."); + ps.println(masterString); + + System.out.println("This server response was written by server TcpExample1."); + System.out.println(masterString); + + ps.flush(); + } + catch(Exception e){ // in case message could not be send + System.out.println("Error while sending message .... press ENTER"); + System.in.read(); + System.exit(0); + + } + clientConnection.close(); + } + catch (IOException e){ // connection could not be established + System.out.println("Connection could not be established. Quit program"); + System.exit(0); + } + + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework1/MahanTCPExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/MahanTCPExample1Telnet.java index 2383eabdd4d2836ba05979a0fb26fe79883ce5ed..187b7b58d51b0b31932d63632c0d273b8023a046 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework1/MahanTCPExample1Telnet.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/MahanTCPExample1Telnet.java @@ -1,61 +1,64 @@ - - -package MV3500Cohort2020JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; -/** - * - * @author Bill - */ -public class MahanTCPExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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 Bill's server TcpExample1."); // to remote clientnc - ps.println("When you see something that is not right, not fair, not just, you have to speak up. You have to say something; you have to do something. "); - System.out.println("This server response was written by Bill's server TcpExample1."); // to server console - System.out.println("John Lewis"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } + + +package MV3500Cohort2020JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; +/** + * + * @author Bill + */ +public class MahanTCPExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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 Bill's server TcpExample1."); // to remote clientnc + ps.println("When you see something that is not right, not fair, not just, you have to speak up. You have to say something; you have to do something. "); + System.out.println("This server response was written by Bill's server TcpExample1."); // to server console + System.out.println("John Lewis"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + if (e instanceof java.net.BindException) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } } \ No newline at end of file diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework1/WeissenbergerTcpExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/WeissenbergerTcpExample1Telnet.java index 319d385bfb9714c46cae16ec9e88e3bb823dfbaf..593449b5830ac33991f24172311945c96357cc5e 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework1/WeissenbergerTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/WeissenbergerTcpExample1Telnet.java @@ -1,63 +1,66 @@ - -package MV3500Cohort2020JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * Simple TCP Telnet Server, MV3500 Homework 1 - * copied parts from: TcpExample1Telnet.java by @author mcgredo and @author brutzman - * @version 07/16/2020 - * @author Loki - */ -public class WeissenbergerTcpExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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 Loki's server TcpExample1."); // to remote clientnc - ps.println(" _ _ _"+"\r\n"+"| | | | (_)"+"\r\n"+"| | ___ | | ___"+"\r\n"+"| |/ _ \\| |/ / |"+"\r\n"+"| | (_) | <| |"+"\r\n"+"|_|\\___/|_|\\_\\_|"); - System.out.println("This server response was written by Loki's server TcpExample1."); // to server console - System.out.println(" _ _ _"+"\n"+"| | | | (_)"+"\n"+"| | ___ | | ___"+"\n"+"| |/ _ \\| |/ / |"+"\n"+"| | (_) | <| |"+"\n"+"|_|\\___/|_|\\_\\_|"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} + +package MV3500Cohort2020JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Simple TCP Telnet Server, MV3500 Homework 1 + * copied parts from: TcpExample1Telnet.java by @author mcgredo and @author brutzman + * @version 07/16/2020 + * @author Loki + */ +public class WeissenbergerTcpExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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 Loki's server TcpExample1."); // to remote clientnc + ps.println(" _ _ _"+"\r\n"+"| | | | (_)"+"\r\n"+"| | ___ | | ___"+"\r\n"+"| |/ _ \\| |/ / |"+"\r\n"+"| | (_) | <| |"+"\r\n"+"|_|\\___/|_|\\_\\_|"); + System.out.println("This server response was written by Loki's server TcpExample1."); // to server console + System.out.println(" _ _ _"+"\n"+"| | | | (_)"+"\n"+"| | ___ | | ___"+"\n"+"| |/ _ \\| |/ / |"+"\n"+"| | (_) | <| |"+"\n"+"|_|\\___/|_|\\_\\_|"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/homework1/WhiteTcpExample1Telnet.java b/assignments/src/MV3500Cohort2020JulySeptember/homework1/WhiteTcpExample1Telnet.java index 68dc004b03f218ec9f65f4909e9ad7cd3be7fc54..aeaf29104108ff95aa91b1cd1a51a99fd04187b4 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework1/WhiteTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework1/WhiteTcpExample1Telnet.java @@ -32,6 +32,8 @@ import java.net.*; */ public class WhiteTcpExample1Telnet { + /** 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/Britt/Britt_Client.java b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Britt/Britt_Client.java index c63847a97b66a608c237ba38bd19d3011c57d858..99209e87d5b600415094b95e520c91aa714a5cef 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework2/Britt/Britt_Client.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework2/Britt/Britt_Client.java @@ -16,7 +16,7 @@ import java.net.Socket; */ public class Britt_Client { -// 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"; /** @@ -36,7 +36,6 @@ public class Britt_Client { OutputStream os; PrintStream ps; - try { while (true) { System.out.println("TCP Homework2 creating socket..."); diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpReceiver.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpReceiver.java index 099e91a9d6638bdd8e5091da2e892d3784132cfe..5fee78c3a77554eb460854e425f2d706d3427a96 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpReceiver.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpReceiver.java @@ -18,7 +18,9 @@ import java.net.*; public class CannonUdpReceiver { // 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"; /** diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpSender.java index 6699b7b5c649307964a5dc072e3196e732b48e48..d43debea1543fa6f93e4bac4a70475e3c85a3cde 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpSender.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Cannon/CannonUdpSender.java @@ -19,10 +19,11 @@ import java.net.*; */ public class CannonUdpSender { - public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) + private 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 + /** socket value of shared interest */ public static final int RECEIVING_PORT = 1415; - public static final int TOTAL_PACKETS_TO_SEND = 100; + private static final int TOTAL_PACKETS_TO_SEND = 100; // here is what we need for lab comms public static final String DESTINATION_HOST = "10.1.105.7"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java index 9391bb86c2817cfc830ecd0e4c6ae725c330512b..28e906467134c24b35af24f960231e20dd447c10 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPReceiverGaribay.java @@ -21,9 +21,8 @@ public class UDPReceiverGaribay /** * @param args command-line arguments - * @throws java.io.IOException */ - public static void main(String[] args) throws IOException + public static void main(String[] args) { DatagramSocket udpSocket = null; diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java index 461af85871491b0b4fbd048b81a44256108d3741..7d08bfd78535ff2838bcce94f79f71bacc564ffb 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/Garibay/UDPSenderGaribay.java @@ -15,10 +15,12 @@ import java.net.InetAddress; 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) + private 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 + /** socket value of shared interest */ public static final int RECEIVING_PORT = 1415; - public static final int TOTAL_PACKETS_TO_SEND = 100; + /** socket value of shared interest */ + private 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 diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java index 87658e1bc040f4669517411e94a77d2dd445c5c3..0fb971caadac84762dec4b25d8bb997ace99446f 100755 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/Cannon/PDUReciever.java @@ -1,132 +1,134 @@ -package MV3500Cohort2020JulySeptember.homework4.Cannon; - -import MV3500Cohort2020JulySeptember.homework4.White.working.*; -import java.io.*; -import java.net.*; -import java.util.*; - -import edu.nps.moves.dis7.pdus.*; -import edu.nps.moves.dis7.utilities.*; - -/** - * Receives PDUs from GermanyEspduReceiverEspduVPNSender in IEEE DIS format. - * - * @version 09/05/2020 - * @author Bernd/Stefan - * @version 0.1 - */ -public class PDUReciever { - - /** - * Max size of a PDU in binary format that we can receive. This is actually - * somewhat outdated--PDUs can be larger--but this is a reasonable starting - * point. - */ - public static final int MAX_PDU_SIZE = 8192; - - /** - * Default port used, matches Wireshark DIS capture default - */ - public static final int DEFAULT_PORT = 2317; - public static final int SECOND_PORT = 3000; - public static final int THIRD_PORT = 2318; - - - /** - * Output prefix to identify this class - */ - private final static String TRACE_PREFIX = "[" + PDUReciever.class.getName() + "] "; - - /** - * Program invocation, execution starts here - * @param args command-line arguments - */ - public static void main(String args[]) { - System.out.println(TRACE_PREFIX + "started..."); - - MulticastSocket socket1; - MulticastSocket socket2; - MulticastSocket socket3; - DatagramPacket packet; - DatagramPacket packet2; - DatagramPacket packet3; - PduFactory pduFactory = new PduFactory(); - ArrayList<EntityID> knownEntities = new ArrayList<EntityID>(); - int pduCount = 0; - - try { - // Specify the socket to receive data - socket1 = new MulticastSocket(DEFAULT_PORT); - socket2 = new MulticastSocket(SECOND_PORT); - socket3 = new MulticastSocket(THIRD_PORT); - - System.out.println(TRACE_PREFIX + "listening for PDU packets on port " + DEFAULT_PORT );//+ " " + SECOND_PORT + " " + THIRD_PORT); - System.out.println("===================================================="); - - while (true) // Loop infinitely, receiving datagrams - { - byte buffer[] = new byte[MAX_PDU_SIZE]; - packet = new DatagramPacket(buffer, buffer.length); - - socket1.receive(packet); - - - - List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(), packet.getLength()); - if (pduBundle.size() > 1) { // should be 1 for this project - System.out.println("Bundle size is " + pduBundle.size()); - } - - // end iterator loop through PDU bundle - for (Pdu aPdu : pduBundle) { - pduCount++; - String receiptMessage = String.format("%3s", pduCount) // right justify, 3 characters - + ". received PDU type " + aPdu.getPduType().getValue() + "=" + aPdu.getPduType().name() + " " + aPdu.getClass().getName() + " from " + packet.getAddress(); - if (aPdu instanceof EntityStatePdu) { - System.out.println(receiptMessage); - EntityID entityID = ((EntityStatePdu) aPdu).getEntityID(); - Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation(); - System.out.println(" entityID triplet: [" + entityID.getSiteID() + ", " + entityID.getApplicationID() + ", " + entityID.getEntityID() + "] "); - if (!knownEntities.contains(entityID)){ - knownEntities.add(entityID); - EntityType entityType = ((EntityStatePdu) aPdu).getEntityType(); - System.out.println(" New Entity: " +entityType.getEntityKind() + " "+ entityType.getDomain() + " "+ entityType.getCountry() + " "+ entityType.getCategory() + " "+ entityType.getSubCategory() + " "+ entityType.getSpecific() ); - } - System.out.println(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); - - } - else if (aPdu instanceof FirePdu){ - System.out.println(receiptMessage); - EntityID firingEntityID = ((FirePdu) aPdu).getFiringEntityID(); - EntityID targetEntityID = ((FirePdu) aPdu).getTargetEntityID(); - MunitionDescriptor munitionDescriptor = ((FirePdu) aPdu).getDescriptor(); - System.out.println(" firingEntityID triplet: [" + firingEntityID.getSiteID() + ", " + firingEntityID.getApplicationID() + ", " + firingEntityID.getEntityID() + "] "); - System.out.println(" targetEntityID triplet: [" + targetEntityID.getSiteID() + ", " + targetEntityID.getApplicationID() + ", " + targetEntityID.getEntityID() + "] "); - System.out.println(" Munition Information: [" + munitionDescriptor.getMunitionType().getDomain() + "."+munitionDescriptor.getMunitionType().getCountry() + "." + munitionDescriptor.getMunitionType().getCategory() + "."+ munitionDescriptor.getMunitionType().getSubCategory() + "." + munitionDescriptor.getMunitionType().getSpecific() + "." + munitionDescriptor.getMunitionType().getExtra() + "]"); - } - else if (aPdu instanceof CommentReliablePdu){ - System.out.println(receiptMessage); - ArrayList<VariableDatum> payloadList = (ArrayList)((CommentReliablePdu) aPdu).getVariableDatumRecords(); - if (!payloadList.isEmpty()) - System.out.print (" messages: "); - for (VariableDatum variableDatum : payloadList) - { - String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String - System.out.print (" \"" + nextComment + "\""); - System.out.println(); - } - } //OTHER PDU TYPES - else { - System.out.println(receiptMessage); - } - } // end of bundle loop - - } // end of while loop - } // end try block // end try block // end try block // end try block - catch (IOException ioe) { - System.out.println(TRACE_PREFIX + "Problem with input/output, see exception trace:"); - System.out.println(ioe); - } - System.out.println(TRACE_PREFIX + "complete."); - } // end main -} // end class +package MV3500Cohort2020JulySeptember.homework4.Cannon; + +import MV3500Cohort2020JulySeptember.homework4.White.working.*; +import java.io.*; +import java.net.*; +import java.util.*; + +import edu.nps.moves.dis7.pdus.*; +import edu.nps.moves.dis7.utilities.*; + +/** + * Receives PDUs from GermanyEspduReceiverEspduVPNSender in IEEE DIS format. + * + * @version 09/05/2020 + * @author Bernd/Stefan + * @version 0.1 + */ +public class PDUReciever { + + /** + * Max size of a PDU in binary format that we can receive. This is actually + * somewhat outdated--PDUs can be larger--but this is a reasonable starting + * point. + */ + public static final int MAX_PDU_SIZE = 8192; + + /** + * Default port used, matches Wireshark DIS capture default + */ + public static final int DEFAULT_PORT = 2317; + /** socket value of shared interest */ + public static final int SECOND_PORT = 3000; + /** socket value of shared interest */ + public static final int THIRD_PORT = 2318; + + + /** + * Output prefix to identify this class + */ + private final static String TRACE_PREFIX = "[" + PDUReciever.class.getName() + "] "; + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String args[]) { + System.out.println(TRACE_PREFIX + "started..."); + + MulticastSocket socket1; + MulticastSocket socket2; + MulticastSocket socket3; + DatagramPacket packet; + DatagramPacket packet2; + DatagramPacket packet3; + PduFactory pduFactory = new PduFactory(); + ArrayList<EntityID> knownEntities = new ArrayList<EntityID>(); + int pduCount = 0; + + try { + // Specify the socket to receive data + socket1 = new MulticastSocket(DEFAULT_PORT); + socket2 = new MulticastSocket(SECOND_PORT); + socket3 = new MulticastSocket(THIRD_PORT); + + System.out.println(TRACE_PREFIX + "listening for PDU packets on port " + DEFAULT_PORT );//+ " " + SECOND_PORT + " " + THIRD_PORT); + System.out.println("===================================================="); + + while (true) // Loop infinitely, receiving datagrams + { + byte buffer[] = new byte[MAX_PDU_SIZE]; + packet = new DatagramPacket(buffer, buffer.length); + + socket1.receive(packet); + + + + List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(), packet.getLength()); + if (pduBundle.size() > 1) { // should be 1 for this project + System.out.println("Bundle size is " + pduBundle.size()); + } + + // end iterator loop through PDU bundle + for (Pdu aPdu : pduBundle) { + pduCount++; + String receiptMessage = String.format("%3s", pduCount) // right justify, 3 characters + + ". received PDU type " + aPdu.getPduType().getValue() + "=" + aPdu.getPduType().name() + " " + aPdu.getClass().getName() + " from " + packet.getAddress(); + if (aPdu instanceof EntityStatePdu) { + System.out.println(receiptMessage); + EntityID entityID = ((EntityStatePdu) aPdu).getEntityID(); + Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation(); + System.out.println(" entityID triplet: [" + entityID.getSiteID() + ", " + entityID.getApplicationID() + ", " + entityID.getEntityID() + "] "); + if (!knownEntities.contains(entityID)){ + knownEntities.add(entityID); + EntityType entityType = ((EntityStatePdu) aPdu).getEntityType(); + System.out.println(" New Entity: " +entityType.getEntityKind() + " "+ entityType.getDomain() + " "+ entityType.getCountry() + " "+ entityType.getCategory() + " "+ entityType.getSubCategory() + " "+ entityType.getSpecific() ); + } + System.out.println(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); + + } + else if (aPdu instanceof FirePdu){ + System.out.println(receiptMessage); + EntityID firingEntityID = ((FirePdu) aPdu).getFiringEntityID(); + EntityID targetEntityID = ((FirePdu) aPdu).getTargetEntityID(); + MunitionDescriptor munitionDescriptor = ((FirePdu) aPdu).getDescriptor(); + System.out.println(" firingEntityID triplet: [" + firingEntityID.getSiteID() + ", " + firingEntityID.getApplicationID() + ", " + firingEntityID.getEntityID() + "] "); + System.out.println(" targetEntityID triplet: [" + targetEntityID.getSiteID() + ", " + targetEntityID.getApplicationID() + ", " + targetEntityID.getEntityID() + "] "); + System.out.println(" Munition Information: [" + munitionDescriptor.getMunitionType().getDomain() + "."+munitionDescriptor.getMunitionType().getCountry() + "." + munitionDescriptor.getMunitionType().getCategory() + "."+ munitionDescriptor.getMunitionType().getSubCategory() + "." + munitionDescriptor.getMunitionType().getSpecific() + "." + munitionDescriptor.getMunitionType().getExtra() + "]"); + } + else if (aPdu instanceof CommentReliablePdu){ + System.out.println(receiptMessage); + ArrayList<VariableDatum> payloadList = (ArrayList)((CommentReliablePdu) aPdu).getVariableDatumRecords(); + if (!payloadList.isEmpty()) + System.out.print (" messages: "); + for (VariableDatum variableDatum : payloadList) + { + String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String + System.out.print (" \"" + nextComment + "\""); + System.out.println(); + } + } //OTHER PDU TYPES + else { + System.out.println(receiptMessage); + } + } // end of bundle loop + + } // end of while loop + } // end try block // end try block // end try block // end try block + catch (IOException ioe) { + System.out.println(TRACE_PREFIX + "Problem with input/output, see exception trace:"); + System.out.println(ioe); + } + System.out.println(TRACE_PREFIX + "complete."); + } // end main +} // end class diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java index 91f18ffa79d3cd8ba9126bd8e399d3b57b10d304..02c665c7ee222eb8b51f601147517ddd9043de22 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FisherTCPExample1Telnet.java @@ -1,64 +1,67 @@ -package MV3500Cohort2021JulySeptember.homework1; - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author adfis - */ - - -public class FisherTCPExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was written by Alex' server TcpExample1."); // to remote clientnc - ps.println("We are crushing this assignment, good work."); - System.out.println("Server response was written by Alex' server TcpExample1."); // to server console - System.out.println("SUCCESS!"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("FisherTCPExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with FisherTCPExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} - +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author adfis + */ + + +public class FisherTCPExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was written by Alex' server TcpExample1."); // to remote clientnc + ps.println("We are crushing this assignment, good work."); + System.out.println("Server response was written by Alex' server TcpExample1."); // to server console + System.out.println("SUCCESS!"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("FisherTCPExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with FisherTCPExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java index 67275fa64d71b19b353d4a84947fa57fab80d2df..bc10d3f03a375cbe549ba81203e6238894866f3b 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Client.java @@ -1,96 +1,97 @@ -package MV3500Cohort2021JulySeptember.homework1; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -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 justi - */ -public class FrankTCPExample3Client { - - /** - */ - public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; - - /** - * Program invocation, execution starts here - * - * @param args command-line arguments - * @throws java.lang.InterruptedException - */ - public static void main(String[] args) throws InterruptedException { - - // Local variables/fields - Socket socket = null; - InputStream is; - Reader isr; - BufferedReader br; - String serverMessage; - int clientLoopCount = 0; - - try { - while (true) { - clientLoopCount++; // increment at beginning of loop for reliability - System.out.println(FrankTCPExample3Client.class.getName() + " 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, 2318); // 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.print("Client loop " + clientLoopCount + ": "); - System.out.println("now we're talking!"); - System.out.println("The message the server sent was: '" + serverMessage + "'"); - // socket gets closed, either automatically/silently by this code (or possibly by the server) - if (serverMessage.equals("this is good bye message from the server")) { //if client recieved termanation message stop client - break; - } - Thread.sleep(1000); // turned it down to 1 second - - } // end while(true) // infinite loops are dangerous, be sure to kill this process! - } catch (IOException e) { - System.err.println("Problem with " + FrankTCPExample3Client.class.getName() + " 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 // occurs after any other activity when shutting down - { - try { - if (socket != null) { - socket.close(); - } - } catch (IOException e) { - } - - // program exit: tell somebody about that happening. Likely cause: server drops connection. - System.out.println(); - System.out.println("FrankTCPExample3Client.class.getName() + exit"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +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 justi + */ +public class FrankTCPExample3Client { + + /** + */ + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + /** + * Program invocation, execution starts here + * + * @param args command-line arguments + */ + public static void main(String[] args) + { + // Local variables/fields + Socket socket = null; + InputStream is; + Reader isr; + BufferedReader br; + String serverMessage; + int clientLoopCount = 0; + + try { + while (true) { + clientLoopCount++; // increment at beginning of loop for reliability + System.out.println(FrankTCPExample3Client.class.getName() + " 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, 2318); // 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.print("Client loop " + clientLoopCount + ": "); + System.out.println("now we're talking!"); + System.out.println("The message the server sent was: '" + serverMessage + "'"); + // socket gets closed, either automatically/silently by this code (or possibly by the server) + if (serverMessage.equals("this is good bye message from the server")) { //if client recieved termanation message stop client + break; + } + Thread.sleep(1000); // turned it down to 1 second + + } // end while(true) // infinite loops are dangerous, be sure to kill this process! + } + catch (IOException | InterruptedException e) + { + System.err.println("Problem with " + FrankTCPExample3Client.class.getName() + " 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 // occurs after any other activity when shutting down + { + try { + if (socket != null) { + socket.close(); + } + } catch (IOException e) { + } + + // program exit: tell somebody about that happening. Likely cause: server drops connection. + System.out.println(); + System.out.println("FrankTCPExample3Client.class.getName() + exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java index 45c8aa10fe6e56ddac91ec864216aac7eb93cafd..4f2f56382d86421fa3a0dca356e9996ed4f8ba23 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/FrankTCPExample3Server.java @@ -1,107 +1,108 @@ -package MV3500Cohort2021JulySeptember.homework1; - -import java.io.*; -import java.net.*; - -/** - * Very slightly more complex than example1, further modifying example2. 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 (nc) localhost 2318 - * - * If you're sophisticated you can contact the instructor's computer while - * running this program. - * - * telnet (nc) [ipNumberOfServerLaptop] 2317 - * - * and have the instructor display the socket pairs received. - * - * @author mcgredo - * @author brutzman - */ -public class FrankTCPExample3Server { - - /** - * Program invocation, execution starts here If already compiled, can run - * using console in directory ../../build/classes/ by invoking \ java - * -classpath . TcpExamples.TcpExample3Server - * - * @param args command-line arguments - */ - public static void main(String[] args) throws InterruptedException { - 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(FrankTCPExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first - - ServerSocket serverSocket = new ServerSocket(2318); // changed from 2317 to 2318 - OutputStream os; - PrintStream ps; - InetAddress localAddress, remoteAddress; - int localPort, remotePort; - int serverLoopCount = 0; - - // 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 clientConnectionSocket = serverSocket.accept()) { - serverLoopCount++; // increment at beginning of loop for reliability - - // Now hook everything up (i.e. set up the streams), Java style: - os = clientConnectionSocket.getOutputStream(); - ps = new PrintStream(os); - if (serverLoopCount <= 20) { // checking if the loop count <= 20 - ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client! - } else { - ps.println("this is good bye message from the server"); // termination after 20 messages - break; // Stop server - } - // Print some information locally about the Socket connection. - // This includes the port and IP numbers on both sides (the socket pair). - localAddress = clientConnectionSocket.getLocalAddress(); - remoteAddress = clientConnectionSocket.getInetAddress(); - localPort = clientConnectionSocket.getLocalPort(); - remotePort = clientConnectionSocket.getPort(); - - System.out.print("Server loop " + serverLoopCount + ": "); - - // 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(FrankTCPExample3Server.class.getName() + " socket pair showing host name, address, port:"); - System.out.println(" (( " - + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " - + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); - - if (localAddress.getHostName().equals(localAddress.getHostAddress()) - || remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) { - System.out.println(" note HostName matches address if host has no DNS name"); - } - - // Not/*i*/ce 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. - ps.flush(); - // like it or not, you're outta here! - } - } - } catch (IOException e) { - System.err.println("Problem with " + FrankTCPExample3Server.class.getName() + " 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 MV3500Cohort2021JulySeptember.homework1; + +import java.io.*; +import java.net.*; + +/** + * Very slightly more complex than example1, further modifying example2. 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 (nc) localhost 2318 + * + * If you're sophisticated you can contact the instructor's computer while + * running this program. + * + * telnet (nc) [ipNumberOfServerLaptop] 2317 + * + * and have the instructor display the socket pairs received. + * + * @author mcgredo + * @author brutzman + */ +public class FrankTCPExample3Server { + + /** + * Program invocation, execution starts here If already compiled, can run + * using console in directory ../../build/classes/ by invoking \ java + * -classpath . TcpExamples.TcpExample3Server + * + * @param args command-line arguments + */ + 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(FrankTCPExample3Server.class.getName() + " has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(2318); // changed from 2317 to 2318 + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + int localPort, remotePort; + int serverLoopCount = 0; + + // 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 clientConnectionSocket = serverSocket.accept()) { + serverLoopCount++; // increment at beginning of loop for reliability + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnectionSocket.getOutputStream(); + ps = new PrintStream(os); + if (serverLoopCount <= 20) { // checking if the loop count <= 20 + ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client! + } else { + ps.println("this is good bye message from the server"); // termination after 20 messages + break; // Stop server + } + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnectionSocket.getLocalAddress(); + remoteAddress = clientConnectionSocket.getInetAddress(); + localPort = clientConnectionSocket.getLocalPort(); + remotePort = clientConnectionSocket.getPort(); + + System.out.print("Server loop " + serverLoopCount + ": "); + + // 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(FrankTCPExample3Server.class.getName() + " socket pair showing host name, address, port:"); + System.out.println(" (( " + + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + + if (localAddress.getHostName().equals(localAddress.getHostAddress()) + || remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) { + System.out.println(" note HostName matches address if host has no DNS name"); + } + + // Not/*i*/ce 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. + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with " + FrankTCPExample3Server.class.getName() + " 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/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java index 3ba4381f1ad60668101abdd5a16b4a0283e5f2cd..9404f23dd9e679af6eb7a4e8388d838a6b149b87 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerDomTcpExample1Telnet.java @@ -1,63 +1,66 @@ -package MV3500Cohort2021JulySeptember.homework1; - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author adfis - */ - - -public class HittnerDomTcpExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was brought to you by the Domo"); // to remote clientnc - ps.println("DomsaMom"); - System.out.println("Client response was brought to you by the Domo"); // to server console - System.out.println("DomsaMom"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author adfis + */ + + +public class HittnerDomTcpExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was brought to you by the Domo"); // to remote clientnc + ps.println("DomsaMom"); + System.out.println("Client response was brought to you by the Domo"); // to server console + System.out.println("DomsaMom"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.java index 495dcb25294407aa06f1e22268a6eb25ebdfdbd7..7a23828f5a550bde530b97b09898a97487f10b61 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/HittnerNickTcpExample1Telnet.java @@ -1,63 +1,66 @@ -package MV3500Cohort2021JulySeptember.homework1; - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author adfis - */ - - -public class HittnerNickTcpExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was brought to you by the Highlander"); // to remote clientnc - ps.println("There Can Be Only One"); - System.out.println("Client response was brought to you by the Highlander"); // to server console - System.out.println("There Can be Only One"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author adfis + */ + + +public class HittnerNickTcpExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was brought to you by the Highlander"); // to remote clientnc + ps.println("There Can Be Only One"); + System.out.println("Client response was brought to you by the Highlander"); // to server console + System.out.println("There Can be Only One"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/MV3500Cohort2021JulySeptember/homework1/KeevenTCPExample3Client.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/KeevenTCPExample3Client.java index baa963c2f6dbaebe3157da7c2e9b31a7e1797322..f318ba5ab1d4d68c6bbbd7f2a889a891ada842b1 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/KeevenTCPExample3Client.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/KeevenTCPExample3Client.java @@ -1,99 +1,100 @@ -package MV3500Cohort2021JulySeptember.homework1; - -import java.io.*; -import java.net.*; - -/** - * This is Assignment 1 where I have modified the given code from TCPExample3Client - * -sleep time has been extended to give more time in between loops - * -Initial print line has been altered to reflect assingnment 1 - * -Loop only runs a finite number of times ,10. - * Before, we always used telnet (netcat) to connect to the server. Here we are - * now writing our own program to do the connection. - * - * As you will see, when we run this after we start the server we will see the - * same string telnet printed, sent by the server. The output at the server will - * show different socket pairs for each time the loop iterates. - * - * @author mcgredo - * @author brutzman - */ -public class KeevenTCPExample3Client { - - /** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 - * @see <a href="https://en.wikipedia.org/wiki/localhost">https://en.wikipedia.org/wiki/localhost</a> - * @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a> - */ - public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; - - /** - * Program invocation, execution starts here - * @param args command-line arguments - */ - public static void main(String[] args) throws InterruptedException { - - // Local variables/fields - Socket socket = null; - InputStream is; - Reader isr; - BufferedReader br; - String serverMessage; - int clientLoopCount = 0; - int numbLoops = 10; - - try { - while (clientLoopCount < numbLoops) - { - clientLoopCount++; // increment at beginning of loop for reliability - System.out.println(KeevenTCPExample3Client.class.getName() + " 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("======================Assignment1==========================="); - - System.out.print ("Client loop " + clientLoopCount + ": "); - System.out.println("now we're talking!"); - System.out.println("The message the server sent was: '" + serverMessage + "'"); - // socket gets closed, either automatically/silently by this code (or possibly by the server) - - Thread.sleep(800l); // slow things down, for example 500l (long) = 500 msec - - } // end while(true) // infinite loops are dangerous, be sure to kill this process! - } - catch (IOException e) - { - System.err.println("Problem with " + KeevenTCPExample3Client.class.getName() + " 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 // occurs after any other activity when shutting down - { - try { - if (socket != null) - socket.close(); - } catch (IOException e) {} - - // program exit: tell somebody about that happening. Likely cause: server drops connection. - System.out.println(); - System.out.println(KeevenTCPExample3Client.class.getName() + " exit"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.*; +import java.net.*; + +/** + * This is Assignment 1 where I have modified the given code from TCPExample3Client + * -sleep time has been extended to give more time in between loops + * -Initial print line has been altered to reflect assingnment 1 + * -Loop only runs a finite number of times ,10. + * Before, we always used telnet (netcat) to connect to the server. Here we are + * now writing our own program to do the connection. + * + * As you will see, when we run this after we start the server we will see the + * same string telnet printed, sent by the server. The output at the server will + * show different socket pairs for each time the loop iterates. + * + * @author mcgredo + * @author brutzman + */ +public class KeevenTCPExample3Client { + + /** IPv6 String constant for localhost address, similarly IPv4 127.0.0.1 + * @see <a href="https://en.wikipedia.org/wiki/localhost">https://en.wikipedia.org/wiki/localhost</a> + * @see <a href="https://en.wikipedia.org/wiki/IPv6_address">https://en.wikipedia.org/wiki/IPv6_address</a> + */ + public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; + + /** + * Program invocation, execution starts here + * @param args command-line arguments + */ + public static void main(String[] args) + { + + // Local variables/fields + Socket socket = null; + InputStream is; + Reader isr; + BufferedReader br; + String serverMessage; + int clientLoopCount = 0; + int numbLoops = 10; + + try { + while (clientLoopCount < numbLoops) + { + clientLoopCount++; // increment at beginning of loop for reliability + System.out.println(KeevenTCPExample3Client.class.getName() + " 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("======================Assignment1==========================="); + + System.out.print ("Client loop " + clientLoopCount + ": "); + System.out.println("now we're talking!"); + System.out.println("The message the server sent was: '" + serverMessage + "'"); + // socket gets closed, either automatically/silently by this code (or possibly by the server) + + Thread.sleep(800l); // slow things down, for example 500l (long) = 500 msec + + } // end while(true) // infinite loops are dangerous, be sure to kill this process! + } + catch (IOException | InterruptedException e) + { + System.err.println("Problem with " + KeevenTCPExample3Client.class.getName() + " 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 // occurs after any other activity when shutting down + { + try { + if (socket != null) + socket.close(); + } catch (IOException e) {} + + // program exit: tell somebody about that happening. Likely cause: server drops connection. + System.out.println(); + System.out.println(KeevenTCPExample3Client.class.getName() + " exit"); + } + } +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java index fc40c12a19f53e231416efc2d81e898277a8e3c0..3ac41ef7ab069ac08cf204be58f669005a4cf497 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/McNeelyTCPExample2.java @@ -1,103 +1,105 @@ -package MV3500Cohort2021JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.InetAddress; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * Very slightly more complex than example1. The only thing this does - * differently is introduce a loop into the response, so you don't - * have to restart the program after one response. Also, it prints - * out the socket pair the server sees. Run the program via telnet - * several times and compare the socket pairs. - * - * <code>telnet localhost 2317</code> - * - * If you're sophisticated you can contact the instructor's computer - * while running this program. - * - * <code>telnet ipOfServersLaptop 2317</code> - * - * And have that machine display the socket pairs received. - * @author mcgredo - * @author brutzman - * @author McNeely - */ -public class McNeelyTCPExample2 -{ - public static void main(String[] args) - { - try - { - System.out.println("Justin McNeely's TcpExample2 has started and is waiting for a connection..."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317) to win..." ); - - // ServerSocket waits for a connection from a client. - // Notice that it is outside the loop; ServerSocket needs to be made only once. - - int connectionCount = 0; // state variable - int totalEntrantCount = 31; // spoofed entrants - - ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. - // of interest: often client doesn't care what port it uses locally when connecting to that server port. - - // Loop, infinitely, waiting for client connections. - // Stop the program somewhere else. - while(true) - { - // blocks! then proceeds once a connection is "accept"ed - try (Socket clientConnection = serverSocket.accept()) { - connectionCount++; // got another one! - - OutputStream os = clientConnection.getOutputStream(); - PrintStream ps = new PrintStream(os); - - ps.println("This client response was written by server " + McNeelyTCPExample2.class.getName()); // to remote client - System.out.println("This server response was written by server " + McNeelyTCPExample2.class.getName()); // to server console - - ps.println("You have attempted " + connectionCount + "times, you are now aplicant number " + totalEntrantCount + " to win. Keep trying!"); - - totalEntrantCount = (totalEntrantCount + 24); - // 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(); // remember the prior question, why are 2 ports different? - - // 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 )) note IPv6 - // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) - // - // Why is first IP/port the same, while the second set has different ports? - - System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + - remoteAddress.toString() + ", " + remotePort + " ))"); - - System.out.println("got another connection! WINNING!, #" + connectionCount); // report progress - - // Notice the use of flush() and close(). Without - // the close() to Socket object may stay open for - // a while after the client has stopped needing this - // connection. Close() explicitly ends the connection. - ps.flush(); - } - } - } - catch(IOException e) - { - System.err.println("Problem with " + McNeelyTCPExample2.class.getName() + " 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!"); - } - } - -} +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Very slightly more complex than example1. The only thing this does + * differently is introduce a loop into the response, so you don't + * have to restart the program after one response. Also, it prints + * out the socket pair the server sees. Run the program via telnet + * several times and compare the socket pairs. + * + * <code>telnet localhost 2317</code> + * + * If you're sophisticated you can contact the instructor's computer + * while running this program. + * + * <code>telnet ipOfServersLaptop 2317</code> + * + * And have that machine display the socket pairs received. + * @author mcgredo + * @author brutzman + * @author McNeely + */ +public class McNeelyTCPExample2 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("Justin McNeely's TcpExample2 has started and is waiting for a connection..."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (nc localhost 2317) or (telnet localhost 2317) to win..." ); + + // ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket needs to be made only once. + + int connectionCount = 0; // state variable + int totalEntrantCount = 31; // spoofed entrants + + ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on. + // of interest: often client doesn't care what port it uses locally when connecting to that server port. + + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while(true) + { + // blocks! then proceeds once a connection is "accept"ed + try (Socket clientConnection = serverSocket.accept()) { + connectionCount++; // got another one! + + OutputStream os = clientConnection.getOutputStream(); + PrintStream ps = new PrintStream(os); + + ps.println("This client response was written by server " + McNeelyTCPExample2.class.getName()); // to remote client + System.out.println("This server response was written by server " + McNeelyTCPExample2.class.getName()); // to server console + + ps.println("You have attempted " + connectionCount + "times, you are now aplicant number " + totalEntrantCount + " to win. Keep trying!"); + + totalEntrantCount = (totalEntrantCount + 24); + // 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(); // remember the prior question, why are 2 ports different? + + // 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 )) note IPv6 + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + // + // Why is first IP/port the same, while the second set has different ports? + + System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + + remoteAddress.toString() + ", " + remotePort + " ))"); + + System.out.println("got another connection! WINNING!, #" + connectionCount); // report progress + + // Notice the use of flush() and close(). Without + // the close() to Socket object may stay open for + // a while after the client has stopped needing this + // connection. Close() explicitly ends the connection. + ps.flush(); + } + } + } + catch(IOException e) + { + System.err.println("Problem with " + McNeelyTCPExample2.class.getName() + " 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!"); + } + } + +} diff --git a/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java index 2758b2dcafecba78d0f32534a03d3aaa2f1934ba..fffbf2d7e4ca972f0c4da1b219af7afc3e3ecda5 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/ReynoldsTcpExample1Telnet1.java @@ -1,63 +1,66 @@ -package MV3500Cohort2021JulySeptember.homework1; - - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author kReynolds - */ - - -public class ReynoldsTcpExample1Telnet1 { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was brought to you by the Domo"); // to remote clientnc - ps.println("DomsaMom"); - System.out.println("Client response was brought to you by the Domo"); // to server console - System.out.println("DomsaMom"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with TcpExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author kReynolds + */ + + +public class ReynoldsTcpExample1Telnet1 +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was brought to you by the Domo"); // to remote clientnc + ps.println("DomsaMom"); + System.out.println("Client response was brought to you by the Domo"); // to server console + System.out.println("DomsaMom"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with TcpExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + 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/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java b/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java index 608af41e7dee3bfa8a370716eb6e513b28aba79f..5c201f36d6ccefeea1bbc402c462c1d852a8f04f 100644 --- a/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java +++ b/assignments/src/MV3500Cohort2021JulySeptember/homework1/RobinsonTcpExample1Telnet.java @@ -1,60 +1,63 @@ -package MV3500Cohort2021JulySeptember.homework1; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.net.ServerSocket; -import java.net.Socket; - -/** - * - * @author mrobi - */ -public class RobinsonTcpExample1Telnet { - public static void main(String[] args) - { - try - { - System.out.println("TcpExample1Telnet has started and is waiting for a connection."); - System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); - System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); - - // The ServerSocket waits for a connection from a client. - // It returns a Socket object when the connection occurs. - ServerSocket serverSocket = new ServerSocket(2317); - - // Use Java io classes to write text (as opposed to - // unknown bytes of some sort) to the client - - // The Socket object represents the connection between - // the server and client, including a full duplex connection - try (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("Client response was written by Matt's server TcpExample1."); // to remote clientnc - ps.println("Can you hear me?"); - System.out.println("Client response was written by Matt's server TcpExample1."); // to server console - System.out.println("Yes"); - - // "flush()" in important in that it forces a write - // across what is in fact a slow connection - ps.flush(); - } - System.out.println("TcpExample1 completed successfully."); - } - catch(IOException e) - { - System.err.println("Problem with FisherTCPExample1Telnet 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 - - // brute force exception checking, can be brittle if exception message changes - // if (e.getMessage().equals("Address already in use: NET_Bind")) - if (e instanceof java.net.BindException) - System.err.println("*** Be sure to stop any other running instances of programs using this port!"); - } - } -} +package MV3500Cohort2021JulySeptember.homework1; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author mrobi + */ +public class RobinsonTcpExample1Telnet +{ + /** run the program + * @param args command-line arguments, string parameters (unused) */ + public static void main(String[] args) + { + try + { + System.out.println("TcpExample1Telnet has started and is waiting for a connection."); + System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet"); + System.out.println(" enter (telnet localhost 2317) or (nc localhost 2317)..." ); + + // The ServerSocket waits for a connection from a client. + // It returns a Socket object when the connection occurs. + ServerSocket serverSocket = new ServerSocket(2317); + + // Use Java io classes to write text (as opposed to + // unknown bytes of some sort) to the client + + // The Socket object represents the connection between + // the server and client, including a full duplex connection + try (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("Client response was written by Matt's server TcpExample1."); // to remote clientnc + ps.println("Can you hear me?"); + System.out.println("Client response was written by Matt's server TcpExample1."); // to server console + System.out.println("Yes"); + + // "flush()" in important in that it forces a write + // across what is in fact a slow connection + ps.flush(); + } + System.out.println("TcpExample1 completed successfully."); + } + catch(IOException e) + { + System.err.println("Problem with FisherTCPExample1Telnet 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 + + // brute force exception checking, can be brittle if exception message changes + // if (e.getMessage().equals("Address already in use: NET_Bind")) + if (e instanceof java.net.BindException) + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } +}