From bc693da0dc4e6e58841e0200a9e1e838e9c79d34 Mon Sep 17 00:00:00 2001 From: leahhedgcorth <leahhedgcorth@drmi-l7410-031.ern.nps.edu> Date: Mon, 1 May 2023 08:36:45 -0700 Subject: [PATCH] Homework 2 --- .../Hedgcorth/MulticastUdpReceiver.java | 26 +++-- .../Hedgcorth/MulticastUdpSender.java | 107 ++++++++++-------- 2 files changed, 74 insertions(+), 59 deletions(-) diff --git a/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpReceiver.java b/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpReceiver.java index c474c486a8..1f121805b0 100644 --- a/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpReceiver.java +++ b/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpReceiver.java @@ -13,21 +13,23 @@ import java.util.logging.Logger; /** * Looks a lot like UdpReceiver. Start this before launching MulticastSender. + * Modified to close after sending 30 packets. * * @author leahhedgcorth */ -public class MulticastUdpReceiver -{ - /** Default constructor */ - public MulticastUdpReceiver() - { +public class MulticastUdpReceiver { + + /** + * Default constructor + */ + public MulticastUdpReceiver() { // default constructor } // reserved range for all IPv4 multicast: 224.0.0.0 through 239.255.255.255 // https://en.wikipedia.org/wiki/Multicast_address // https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml private static final String MULTICAST_ADDRESS = "239.1.2.15"; - private static final int DESTINATION_PORT = 1718; + private static final int DESTINATION_PORT = 1718; /** * Time to live: how many router-decrement levels can be crossed @@ -39,15 +41,15 @@ public class MulticastUdpReceiver /** * Program invocation, execution starts here + * * @param args command-line arguments */ - public static void main(String[] args) - { + public static void main(String[] args) { MulticastSocket multicastSocket = null; InetSocketAddress group = null; ByteBuffer buffer = ByteBuffer.allocate(1500); DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity()); - + try { System.out.println("UdpExamples.MulticastUdpReceiver started..."); @@ -61,13 +63,13 @@ public class MulticastUdpReceiver multicastSocket = new MulticastSocket(DESTINATION_PORT); multicastSocket.setTimeToLive(TTL); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); - + group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); - + // Join group useful on receiving side multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); // You can join multiple groups here - + System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); System.out.println("Multicast packets received log:"); diff --git a/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpSender.java b/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpSender.java index 5f11529851..f592e12224 100644 --- a/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpSender.java +++ b/assignments/src/MV3500Cohort2023MarchJune/homework2/Hedgcorth/MulticastUdpSender.java @@ -1,4 +1,3 @@ - package MV3500Cohort2023MarchJune.homework2.Hedgcorth; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; @@ -14,60 +13,78 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * Looks a lot like UdpSender. Start this after launching MulticastReceiver. - * - * Privacy note: this sender advertises your user name as part of the multicast packet message - * + * Looks a lot like UdpSender. Start this after launching MulticastReceiver. + * Modified to close after sending 30 packets. + * + * Privacy note: this sender advertises your user name as part of the multicast + * packet message + * * * @author leahhedgcorth */ public class MulticastUdpSender { - - /** Default constructor */ - public MulticastUdpSender() - { + + /** + * Default constructor + */ + public MulticastUdpSender() { // default constructor } // reserved range for all IPv4 multicast: 224.0.0.0 through 239.255.255.255 // https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml - /** Default multicast group address for send and receive connections. - * @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */ + /** + * Default multicast group address for send and receive connections. + * + * @see + * <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> + */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; // within reserved multicast address range - /** Default socket port used, matches Wireshark DIS capture default - * @see <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> */ - public static final int DESTINATION_PORT = 1718; - - /** Time to live: how many router-decrement levels can be crossed */ + /** + * Default socket port used, matches Wireshark DIS capture default + * + * @see + * <a href="https://en.wikipedia.org/wiki/Port_(computer_networking)">https://en.wikipedia.org/wiki/Port_(computer_networking)</a> + */ + public static final int DESTINATION_PORT = 1718; + + /** + * Time to live: how many router-decrement levels can be crossed + */ public static final int TTL = 10; - - /** How many packets to send prior to termination */ - public static final int LOOPSIZE = 20; // or maybe 20000 - - /** Receiving this message causes termination - * @see <a href="https://en.wikipedia.org/wiki/Sentinel_value">https://en.wikipedia.org/wiki/Sentinel_value</a> */ + + /** + * How many packets to send prior to termination + */ + public static final int LOOPSIZE = 30; // or maybe 20000 + + /** + * Receiving this message causes termination + * + * @see + * <a href="https://en.wikipedia.org/wiki/Sentinel_value">https://en.wikipedia.org/wiki/Sentinel_value</a> + */ public static final String QUIT_SENTINEL = "QUIT QUIT QUIT!"; private static NetworkInterface ni; - + /** * Program invocation, execution starts here + * * @param args command-line arguments * @throws java.io.IOException execution error */ @SuppressWarnings("SleepWhileInLoop") - public static void main(String[] args) throws IOException - { + public static void main(String[] args) throws IOException { MulticastSocket multicastSocket = null; InetSocketAddress group = null; - + // 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); - - try - { + DataOutputStream dos = new DataOutputStream(baos); + + try { System.out.println("UdpExamples.MulticastUdpSender started..."); // This is a java/IPv6 problem. You should also add it to the // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true @@ -75,35 +92,33 @@ public class MulticastUdpSender { // 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"); - + // multicast group we are sending to--not a single host multicastSocket = new MulticastSocket(/*DESTINATION_PORT*/); multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); - + group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); // Join group useful on receiving side multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); // You can join multiple groups here - + byte[] buffer = baos.toByteArray(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group/*, DESTINATION_PORT*/); - - for (int index = 0; index < LOOPSIZE; index++) - { + + for (int index = 0; index < LOOPSIZE; index++) { // Put together an updated packet to send - if (index < LOOPSIZE - 1) - { + if (index < LOOPSIZE - 1) { // Put together an updated packet to send dos.writeChars("From " + System.getProperty("user.name") + ": "); dos.writeChars("MulticastSender packet " + Integer.toString(index) + ";"); // string chars for readability - dos.writeInt (index); // arbitrary data, needs Java or byte-alignment to read + dos.writeInt(index); // arbitrary data, needs Java or byte-alignment to read dos.writeFloat(17.0f); // arbitrary data, needs Java or byte-alignment to read dos.writeFloat(23.0f); // arbitrary data, needs Java or byte-alignment to read + } else { + dos.writeChars(QUIT_SENTINEL + ";"); // note string must include ; semicolon as termination sentinel } - else dos.writeChars(QUIT_SENTINEL + ";"); // note string must include ; semicolon as termination sentinel - buffer = baos.toByteArray(); packet.setData(buffer); multicastSocket.send(packet); @@ -118,13 +133,11 @@ public class MulticastUdpSender { Thread.sleep(1000); // Send 100, one per second } System.out.println("MulticastSender complete."); - } - catch(IOException | InterruptedException e) - { + } catch (IOException | InterruptedException e) { System.err.println("Problem with MulticastSender, see exception trace:"); System.err.println(e); } finally { - + if (multicastSocket != null && !multicastSocket.isClosed()) { try { multicastSocket.leaveGroup(group, ni); @@ -133,9 +146,9 @@ public class MulticastUdpSender { } multicastSocket.close(); } - + dos.close(); } } - + } -- GitLab