diff --git a/projects/Assignments/homework2/AngelAssignment 2.pptx b/projects/Assignments/homework2/AngelAssignment 2.pptx new file mode 100644 index 0000000000000000000000000000000000000000..9b88f3aaa11439addbe0d16fd4102f3356c90fab Binary files /dev/null and b/projects/Assignments/homework2/AngelAssignment 2.pptx differ diff --git a/projects/Assignments/homework2/AngelMulticastReceiver.java b/projects/Assignments/homework2/AngelMulticastReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..02668d960ff355f4459d1dc7b0a733bf8d2ace59 --- /dev/null +++ b/projects/Assignments/homework2/AngelMulticastReceiver.java @@ -0,0 +1,68 @@ + + + +import java.io.*; +import java.net.*; + +/** + * + * @author mcgredo + */ +public class AngelMulticastReceiver { + + 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 + while(true) + { + byte[] packetArray = new byte[1500]; + DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); + multicastSocket.receive(packet); + ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); + DataInputStream dis = new DataInputStream(bais); + char type = dis.readChar(); + if (type == 'A') { + float firstNumber = dis.readFloat(); + float secondNumber = dis.readFloat(); + float thirdNumber = dis.readFloat(); + System.out.println("Abrams POS: (x: " + firstNumber + ", y: " + secondNumber +", z: " + thirdNumber + " )"); + } + if (type == 'B') { + int lngth = dis.readInt(); + String informationLine = ""; + for(int i=0; i<lngth; i++) { + informationLine += dis.readChar(); + } + System.out.println(informationLine); + } + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/projects/Assignments/homework2/AngelMulticastSenderExample.java b/projects/Assignments/homework2/AngelMulticastSenderExample.java new file mode 100644 index 0000000000000000000000000000000000000000..5d2c37d512f92935ecf45ddfcf1fee49c8c48644 --- /dev/null +++ b/projects/Assignments/homework2/AngelMulticastSenderExample.java @@ -0,0 +1,92 @@ + + + +import java.io.*; +import java.net.*; +import java.util.Random; + +/** + * Looks a lot like a UDP sender. + * + * @author mcgredo + */ +public class AngelMulticastSenderExample { + + 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(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); + Random randomFLT = new Random(); + Random rndFLT = new Random(); + for(int idx = 0; idx < 100; idx++){ + float flt = rndFLT.nextFloat(); + if (flt <=.5){ + dos.writeChars("A"); + dos.writeFloat(randomFLT.nextFloat()*100); + dos.writeFloat(randomFLT.nextFloat()*100); + dos.writeFloat(randomFLT.nextFloat()*100); + } + else if (flt < .8 ) { + dos.writeChars("B"); + dos.writeInt("Information Bravo.".length()); + dos.writeChars("Information Bravo."); + } + else { + dos.writeChars("B"); + dos.writeInt("Information Charlie.".length()); + dos.writeChars("Information Charlie."); + } + 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? + + + multicastSocket.send(packet); + baos.reset(); + Thread.sleep(1000); // Send 100, one per second + System.out.println("Sent multicast packet " + idx + " of 100"); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +}