From fb7b6bba4261b64e463a18a875cd3ce19d553665 Mon Sep 17 00:00:00 2001
From: "Norbraten, Terry" <tdnorbra@nps.edu>
Date: Tue, 28 Jul 2020 15:43:15 -0700
Subject: [PATCH] improved looping constructs

---
 .../MulticastSender.java                      | 18 ++++++-------
 .../UdpMulticastHttpExamples/UdpReceiver.java | 27 ++++++++-----------
 .../UdpMulticastHttpExamples/UdpSender.java   | 14 +++++-----
 3 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/examples/src/UdpMulticastHttpExamples/MulticastSender.java b/examples/src/UdpMulticastHttpExamples/MulticastSender.java
index 7aaa9e4761..584f01d568 100644
--- a/examples/src/UdpMulticastHttpExamples/MulticastSender.java
+++ b/examples/src/UdpMulticastHttpExamples/MulticastSender.java
@@ -61,8 +61,8 @@ public class MulticastSender {
             multicastSocket.joinGroup(group, DisThreadedNetIF.findIpv4Interface());
             // You can join multiple groups here
             
-            DatagramPacket packet;
-            byte[] buffer;
+            byte[] buffer = baos.toByteArray();
+            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
             
             for (int index = 0; index < LOOPSIZE; index++)
             {
@@ -70,17 +70,16 @@ public class MulticastSender {
                 if (index < LOOPSIZE - 1)
                 {
                     // Put together an updated packet to send
-                    dos.writeChars(System.getProperty("user.name") + ": ");
+                    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.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
-
+                
                 buffer = baos.toByteArray();
-                packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
-
+                packet.setData(buffer);
                 multicastSocket.send(packet);
                 System.out.println("Sent multicast packet " + index + " of " + LOOPSIZE);
                 baos.reset();
@@ -96,13 +95,14 @@ public class MulticastSender {
         }
         catch(IOException | InterruptedException e)
         {
+            System.err.println("Problem with MulticastSender, see exception trace:");
+            System.err.println(e);
+        } finally {
+            
             if (multicastSocket != null)
                 multicastSocket.close();
             
             dos.close();
-            
-            System.err.println("Problem with MulticastSender, see exception trace:");
-            System.err.println(e);
         }
     }
     
diff --git a/examples/src/UdpMulticastHttpExamples/UdpReceiver.java b/examples/src/UdpMulticastHttpExamples/UdpReceiver.java
index 5b23f76d15..829e2c19e3 100644
--- a/examples/src/UdpMulticastHttpExamples/UdpReceiver.java
+++ b/examples/src/UdpMulticastHttpExamples/UdpReceiver.java
@@ -2,6 +2,7 @@ package UdpMulticastHttpExamples;
 
 import java.io.*;
 import java.net.*;
+import java.nio.ByteBuffer;
 
 /**
  * An example of receiving UDP packets. Since very often both the
@@ -26,7 +27,6 @@ public class UdpReceiver
     public static void main(String[] args) throws IOException 
     {
         DatagramSocket udpSocket = null;
-        DataInputStream dis = null;
         int packetCount = 0;
         
         try
@@ -38,10 +38,9 @@ public class UdpReceiver
             udpSocket.setReceiveBufferSize(1500);
             udpSocket.setBroadcast(false); // we're just receiving here
             
-            byte[] receiveBuffer = new byte[udpSocket.getReceiveBufferSize()];
-            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
+            ByteBuffer buffer = ByteBuffer.allocate(1500);
+            DatagramPacket receivePacket = new DatagramPacket(buffer.array(), buffer.capacity());
             
-            InputStream bais;
             float first, second;
             
             // You need a new receiving packet to read from every packet received
@@ -49,27 +48,23 @@ public class UdpReceiver
             {
                 udpSocket.receive(receivePacket);
                 
-                // Decode the contents by extracting the data from the packet
-                bais = new ByteArrayInputStream(receivePacket.getData());
-                dis = new DataInputStream(bais);
-                
                 // What happens if you read an integer? Two double values? ***
-                first = dis.readFloat(); // alternatives: readFloat(); readInt(); dis.readUTF(); 
-                second = dis.readFloat();
+                first = buffer.getFloat(); // alternatives: readFloat(); readInt(); dis.readUTF(); 
+                second = buffer.getFloat();
+                
+                buffer.clear();
                 
                 System.out.println("first value: " + first + " second value: " + second + " packet count = " + ++packetCount);
             }
         }
         catch(IOException e)
         {
-            if (udpSocket != null)
-                udpSocket.close();
-            
-            if (dis != null)
-                dis.close();
-            
             System.err.println("Problem with UdpReceiver, see exception trace:");
             System.err.println(e);
+        } finally {
+            
+            if (udpSocket != null)
+                udpSocket.close();
         }
     }
 }
diff --git a/examples/src/UdpMulticastHttpExamples/UdpSender.java b/examples/src/UdpMulticastHttpExamples/UdpSender.java
index 66ca17a994..d40a56cd97 100644
--- a/examples/src/UdpMulticastHttpExamples/UdpSender.java
+++ b/examples/src/UdpMulticastHttpExamples/UdpSender.java
@@ -16,7 +16,7 @@ import java.net.*;
  */
 public class UdpSender 
 {
-    public static final String          MY_NAME = "(default name)";
+    public static final String          MY_NAME = System.getProperty("user.name");
     public static final int        SENDING_PORT = 1414;
     public static final int      RECEIVING_PORT = 1415;
     public static final String DESTINATION_HOST = "localhost";
@@ -38,7 +38,7 @@ public class UdpSender
             // 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();
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(1500);
             dos = new DataOutputStream(baos);
 			
             // alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
@@ -53,7 +53,7 @@ public class UdpSender
             // ID of the host we are sending from
             InetAddress      sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified
             
-            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, destinationAddress, RECEIVING_PORT );
+            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, destinationAddress, RECEIVING_PORT);
        
             // How fast does this go? Does UDP try to slow it down, or does
             // this cause network problems? (hint: yes for an unlimited send
@@ -65,20 +65,20 @@ public class UdpSender
             {
                udpSocket.send(packet);
                Thread.sleep(1000); // Send 100, one per second
-               System.out.println(MY_NAME + " " + sourceAddress + " sent packet " + index + " of 100");
+               System.out.println(MY_NAME + ": " + sourceAddress + " sent packet " + index + " of 100");
             }
             System.out.println("UdpSender complete.");
         }
         catch (IOException | InterruptedException e)
         {
+            System.err.println("Problem with UdpSender, see exception trace:");
+            System.err.println(e);
+        } finally {
             if (udpSocket != null)
                 udpSocket.close();
             
             if (dos != null)
                 dos.close();
-            
-            System.err.println("Problem with UdpSender, see exception trace:");
-            System.err.println(e);
         }
     }
 }
-- 
GitLab