diff --git a/projects/Assignments/homework2/Sasala UML Diagram & Screen Shot (HW2).docx b/projects/Assignments/homework2/Sasala UML Diagram & Screen Shot (HW2).docx new file mode 100644 index 0000000000000000000000000000000000000000..211194bbc15369dccefa6c4c12c1173c55f91352 Binary files /dev/null and b/projects/Assignments/homework2/Sasala UML Diagram & Screen Shot (HW2).docx differ diff --git a/projects/Assignments/homework2/SasalaMulticastReceiver.java b/projects/Assignments/homework2/SasalaMulticastReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..5effc77a951fe3c571b9fe4dc0b889c4bb809e19 --- /dev/null +++ b/projects/Assignments/homework2/SasalaMulticastReceiver.java @@ -0,0 +1,60 @@ + + + +import java.io.*; +import java.net.*; + +/** + * + * @author mcgredo + */ +public class SasalaMulticastReceiver { + + 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); + float temperature = dis.readFloat(); + String DTG = dis.readLine(); + + System.out.println("Time: " + DTG + " Temperature:" + temperature); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/projects/Assignments/homework2/SasalaMulticastSender.java b/projects/Assignments/homework2/SasalaMulticastSender.java new file mode 100644 index 0000000000000000000000000000000000000000..554b59fa7a678ed39c71ec2f4d608fd3e2bc3079 --- /dev/null +++ b/projects/Assignments/homework2/SasalaMulticastSender.java @@ -0,0 +1,74 @@ + + + +import java.io.*; +import java.net.*; +import java.time.LocalDateTime; + +/** + * Looks a lot like a UDP sender. + * + * @author mcgredo + */ +public class SasalaMulticastSender { + + 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); + + for(int idx = 0; idx < 100; idx++) + { + double temperature = 100 * Math.random(); + LocalDateTime LDT = LocalDateTime.now(); + String time = LDT.toString(); + + dos.writeFloat((float) temperature); + dos.writeChars(time); + + // Put together a packet to send + // muticast group we are sending to--not a single host + byte[] buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + + multicastSocket.send(packet); + Thread.sleep(1000); // Send 100, one per second + System.out.println("Sent multicast packet " + idx + " of 100"); + + baos.reset(); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +}