diff --git a/projects/Assignments/homework2/Hanley_README_MulticastHW.txt b/projects/Assignments/homework2/Hanley_README_MulticastHW.txt new file mode 100644 index 0000000000000000000000000000000000000000..f5daac6fdb9e8c2c8f6babbcbae55471257db9df --- /dev/null +++ b/projects/Assignments/homework2/Hanley_README_MulticastHW.txt @@ -0,0 +1,35 @@ +Hanley README for Multicast Homework + +Modified the code from multicast example 1. + +- adjusted packet to get compiled for every send +- computed the "position" of the entity for each packet +- shout out to Chris A for identifying the need to change the receiver to listen for doubles and not floats + + +********** System outputs for classes: + +Sender: +run-single: +/239.1.2.15 +Sent multicast packet 0 of 100 +Sent multicast packet 1 of 100 +Sent multicast packet 2 of 100 +Sent multicast packet 3 of 100 +Sent multicast packet 4 of 100 +Sent multicast packet 5 of 100 +Sent multicast packet 6 of 100 +Sent multicast packet 7 of 100 + + +Receiver: +run-single: +/239.1.2.15 +Number received: 1 X Pos: 1.4142135623730951 Y Pos: 3.414213562373095 +Number received: 2 X Pos: 2.8284271247461903 Y Pos: 4.82842712474619 +Number received: 3 X Pos: 4.242640687119286 Y Pos: 6.242640687119285 +Number received: 4 X Pos: 5.656854249492381 Y Pos: 7.65685424949238 +Number received: 5 X Pos: 7.0710678118654755 Y Pos: 9.071067811865476 +Number received: 6 X Pos: 8.485281374238571 Y Pos: 10.485281374238571 +Number received: 7 X Pos: 9.899494936611667 Y Pos: 11.899494936611667 +Number received: 8 X Pos: 11.313708498984763 Y Pos: 13.313708498984763 \ No newline at end of file diff --git a/projects/Assignments/homework2/Hanley_multicastHW_receiver.java b/projects/Assignments/homework2/Hanley_multicastHW_receiver.java new file mode 100644 index 0000000000000000000000000000000000000000..c42d2e88afc6ee125aa45e68376c03b65cf36832 --- /dev/null +++ b/projects/Assignments/homework2/Hanley_multicastHW_receiver.java @@ -0,0 +1,63 @@ +/** + * This assignment uses the code from MV3500 example "multicastExample1" + * modifications were made to way the packet is read to facilitate the double + * instead of the float. + * + * @author Brian + */ +import java.io.*; +import java.net.*; + + +public class Hanley_multicastHW_receiver { + 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); + double firstNumber = dis.readDouble(); + double secondNumber = dis.readDouble(); + + System.out.println("Number received: " + count + " X Pos: " + firstNumber + " Y Pos: " + secondNumber); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/projects/Assignments/homework2/Hanley_multicastHW_sender.java b/projects/Assignments/homework2/Hanley_multicastHW_sender.java new file mode 100644 index 0000000000000000000000000000000000000000..e934007ba2f0767ed4f7252878ddd707e8d4c837 --- /dev/null +++ b/projects/Assignments/homework2/Hanley_multicastHW_sender.java @@ -0,0 +1,85 @@ +/** + * This assignment uses the code from MV3500 example "multicastExample1" + * modifications were made to the packet creation and where the packet is + * assembled. + * + * @author Brian + */ + + +import java.io.*; +import java.net.*; + + +public class Hanley_multicastHW_sender { + 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; + private int velocity; + private float xPos; + private float yPos; + + + public static void main(String[] args) + { + int velocity = 2; + int dir = 45; + double xPos = 0; + double yPos = 2; + + + 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 + + + // 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 < 100; idx++) + { + + xPos = xPos + (Math.cos(Math.toRadians(dir))*velocity); //computes the horixontal displacement for each movement based on velocity - this is a fixed velocity and time increment + yPos = yPos + (Math.sin(Math.toRadians(dir))*velocity); //computes the vertical displacement for each movement based on velocity - this is a fixed velocity and time increment + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + dos.writeDouble(xPos); + dos.writeDouble(yPos); + + + byte[] buffer = baos.toByteArray(); + + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + + 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); + } + } + +}