Skip to content
Snippets Groups Projects
Commit bc693da0 authored by leahhedgcorth's avatar leahhedgcorth
Browse files

Homework 2

parent c907aad6
No related branches found
No related tags found
No related merge requests found
...@@ -13,21 +13,23 @@ import java.util.logging.Logger; ...@@ -13,21 +13,23 @@ import java.util.logging.Logger;
/** /**
* Looks a lot like UdpReceiver. Start this before launching MulticastSender. * Looks a lot like UdpReceiver. Start this before launching MulticastSender.
* Modified to close after sending 30 packets.
* *
* @author leahhedgcorth * @author leahhedgcorth
*/ */
public class MulticastUdpReceiver public class MulticastUdpReceiver {
{
/** Default constructor */ /**
public MulticastUdpReceiver() * Default constructor
{ */
public MulticastUdpReceiver() {
// default constructor // default constructor
} }
// reserved range for all IPv4 multicast: 224.0.0.0 through 239.255.255.255 // reserved range for all IPv4 multicast: 224.0.0.0 through 239.255.255.255
// https://en.wikipedia.org/wiki/Multicast_address // https://en.wikipedia.org/wiki/Multicast_address
// https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml // https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
private static final String MULTICAST_ADDRESS = "239.1.2.15"; 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 * Time to live: how many router-decrement levels can be crossed
...@@ -39,15 +41,15 @@ public class MulticastUdpReceiver ...@@ -39,15 +41,15 @@ public class MulticastUdpReceiver
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
*
* @param args command-line arguments * @param args command-line arguments
*/ */
public static void main(String[] args) public static void main(String[] args) {
{
MulticastSocket multicastSocket = null; MulticastSocket multicastSocket = null;
InetSocketAddress group = null; InetSocketAddress group = null;
ByteBuffer buffer = ByteBuffer.allocate(1500); ByteBuffer buffer = ByteBuffer.allocate(1500);
DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity()); DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity());
try { try {
System.out.println("UdpExamples.MulticastUdpReceiver started..."); System.out.println("UdpExamples.MulticastUdpReceiver started...");
...@@ -61,13 +63,13 @@ public class MulticastUdpReceiver ...@@ -61,13 +63,13 @@ public class MulticastUdpReceiver
multicastSocket = new MulticastSocket(DESTINATION_PORT); multicastSocket = new MulticastSocket(DESTINATION_PORT);
multicastSocket.setTimeToLive(TTL); multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); group = new InetSocketAddress(multicastAddress, DESTINATION_PORT);
// Join group useful on receiving side // Join group useful on receiving side
multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface());
// You can join multiple groups here // You can join multiple groups here
System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT);
System.out.println("Multicast packets received log:"); System.out.println("Multicast packets received log:");
......
package MV3500Cohort2023MarchJune.homework2.Hedgcorth; package MV3500Cohort2023MarchJune.homework2.Hedgcorth;
import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface; import edu.nps.moves.dis7.utilities.DisThreadedNetworkInterface;
...@@ -14,60 +13,78 @@ import java.util.logging.Level; ...@@ -14,60 +13,78 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* Looks a lot like UdpSender. Start this after launching MulticastReceiver. * 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 *
* * Privacy note: this sender advertises your user name as part of the multicast
* packet message
*
* *
* @author leahhedgcorth * @author leahhedgcorth
*/ */
public class MulticastUdpSender { public class MulticastUdpSender {
/** Default constructor */ /**
public MulticastUdpSender() * Default constructor
{ */
public MulticastUdpSender() {
// default constructor // default constructor
} }
// reserved range for all IPv4 multicast: 224.0.0.0 through 239.255.255.255 // 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 // 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 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> */ * Default socket port used, matches Wireshark DIS capture default
public static final int DESTINATION_PORT = 1718; *
* @see
/** Time to live: how many router-decrement levels can be crossed */ * <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; public static final int TTL = 10;
/** How many packets to send prior to termination */ /**
public static final int LOOPSIZE = 20; // or maybe 20000 * How many packets to send prior to termination
*/
/** Receiving this message causes termination public static final int LOOPSIZE = 30; // or maybe 20000
* @see <a href="https://en.wikipedia.org/wiki/Sentinel_value">https://en.wikipedia.org/wiki/Sentinel_value</a> */
/**
* 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!"; public static final String QUIT_SENTINEL = "QUIT QUIT QUIT!";
private static NetworkInterface ni; private static NetworkInterface ni;
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
*
* @param args command-line arguments * @param args command-line arguments
* @throws java.io.IOException execution error * @throws java.io.IOException execution error
*/ */
@SuppressWarnings("SleepWhileInLoop") @SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException {
{
MulticastSocket multicastSocket = null; MulticastSocket multicastSocket = null;
InetSocketAddress group = null; InetSocketAddress group = null;
// Put together a message with binary content. "ByteArrayOutputStream" // Put together a message with binary content. "ByteArrayOutputStream"
// is a java.io utility that lets us put together an array of binary // is a java.io utility that lets us put together an array of binary
// data, which we put into the UDP packet. // data, which we put into the UDP packet.
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
try try {
{
System.out.println("UdpExamples.MulticastUdpSender started..."); System.out.println("UdpExamples.MulticastUdpSender started...");
// This is a java/IPv6 problem. You should also add it to the // This is a java/IPv6 problem. You should also add it to the
// arguments used to start the app, eg -Djava.net.preferIPv4Stack=true // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true
...@@ -75,35 +92,33 @@ public class MulticastUdpSender { ...@@ -75,35 +92,33 @@ public class MulticastUdpSender {
// netbeans must be restarted after these settings. // netbeans must be restarted after these settings.
// https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache
System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv4Stack", "true");
// multicast group we are sending to--not a single host // multicast group we are sending to--not a single host
multicastSocket = new MulticastSocket(/*DESTINATION_PORT*/); multicastSocket = new MulticastSocket(/*DESTINATION_PORT*/);
multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission multicastSocket.setTimeToLive(TTL); // time to live reduces scope of transmission
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT); System.out.println("Multicast address/port: " + multicastAddress.getHostAddress() + "/" + DESTINATION_PORT);
group = new InetSocketAddress(multicastAddress, DESTINATION_PORT); group = new InetSocketAddress(multicastAddress, DESTINATION_PORT);
// Join group useful on receiving side // Join group useful on receiving side
multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface()); multicastSocket.joinGroup(group, ni = DisThreadedNetworkInterface.findIpv4Interface());
// You can join multiple groups here // You can join multiple groups here
byte[] buffer = baos.toByteArray(); byte[] buffer = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group/*, DESTINATION_PORT*/); 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 // Put together an updated packet to send
if (index < LOOPSIZE - 1) if (index < LOOPSIZE - 1) {
{
// Put together an updated packet to send // Put together an updated packet to send
dos.writeChars("From " + System.getProperty("user.name") + ": "); dos.writeChars("From " + System.getProperty("user.name") + ": ");
dos.writeChars("MulticastSender packet " + Integer.toString(index) + ";"); // string chars for readability 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(17.0f); // arbitrary data, needs Java or byte-alignment to read
dos.writeFloat(23.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(); buffer = baos.toByteArray();
packet.setData(buffer); packet.setData(buffer);
multicastSocket.send(packet); multicastSocket.send(packet);
...@@ -118,13 +133,11 @@ public class MulticastUdpSender { ...@@ -118,13 +133,11 @@ public class MulticastUdpSender {
Thread.sleep(1000); // Send 100, one per second Thread.sleep(1000); // Send 100, one per second
} }
System.out.println("MulticastSender complete."); 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("Problem with MulticastSender, see exception trace:");
System.err.println(e); System.err.println(e);
} finally { } finally {
if (multicastSocket != null && !multicastSocket.isClosed()) { if (multicastSocket != null && !multicastSocket.isClosed()) {
try { try {
multicastSocket.leaveGroup(group, ni); multicastSocket.leaveGroup(group, ni);
...@@ -133,9 +146,9 @@ public class MulticastUdpSender { ...@@ -133,9 +146,9 @@ public class MulticastUdpSender {
} }
multicastSocket.close(); multicastSocket.close();
} }
dos.close(); dos.close();
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment