diff --git a/projects/Assignments/homework2/TackettMultiCastReceiver.java b/projects/Assignments/homework2/TackettMultiCastReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..1685fc3f590d9c7bb31e183c06a20d7b0c8d1969 --- /dev/null +++ b/projects/Assignments/homework2/TackettMultiCastReceiver.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 codyt + */ +import java.io.*; +import java.net.*; + + +public class TackettMultiCastReceiver { + 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/TackettMulticastSender.java b/projects/Assignments/homework2/TackettMulticastSender.java new file mode 100644 index 0000000000000000000000000000000000000000..6942dfa6743786eb8f51464d0ccadab51a956aaa --- /dev/null +++ b/projects/Assignments/homework2/TackettMulticastSender.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 TackettMulticastSender { + 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 = 3; + int direction = 180; + 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(direction))*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(direction))*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); + } + } + +}