From 46c9efab50810dd755087c97a3bc06f9e7082b56 Mon Sep 17 00:00:00 2001
From: "Norbraten, Terry" <tdnorbra@nps.edu>
Date: Mon, 27 Jul 2020 13:34:47 -0700
Subject: [PATCH] no object creation in loops

---
 .../UdpMulticastHttpExamples/UdpReceiver.java | 44 ++++++++++++++-----
 .../UdpMulticastHttpExamples/UdpSender.java   | 27 ++++++++----
 2 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/examples/src/UdpMulticastHttpExamples/UdpReceiver.java b/examples/src/UdpMulticastHttpExamples/UdpReceiver.java
index 178b785722..5b23f76d15 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.*;
+
 /**
  * An example of receiving UDP packets. Since very often both the
  * sender and receiver are on the same host we use different ports
@@ -20,36 +21,55 @@ public class UdpReceiver
 
     /**
      * @param args the command line arguments
+     * @throws java.io.IOException
      */
-    public static void main(String[] args) 
+    public static void main(String[] args) throws IOException 
     {
+        DatagramSocket udpSocket = null;
+        DataInputStream dis = null;
+        int packetCount = 0;
+        
         try
         {
-            System.out.println("UdpReceiver started...");
+            System.out.println(UdpReceiver.class.getName() + " started...");
+            
             // Create a UDP socket
-            DatagramSocket udpSocket = new DatagramSocket(RECEIVING_PORT);
+            udpSocket = new DatagramSocket(RECEIVING_PORT);
+            udpSocket.setReceiveBufferSize(1500);
+            udpSocket.setBroadcast(false); // we're just receiving here
+            
+            byte[] receiveBuffer = new byte[udpSocket.getReceiveBufferSize()];
+            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
+            
+            InputStream bais;
+            float first, second;
             
             // You need a new receiving packet to read from every packet received
             while (true)
             {
-                byte[] receiveBuffer = new byte[1500];
-                DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
                 udpSocket.receive(receivePacket);
                 
                 // Decode the contents by extracting the data from the packet
-                ByteArrayInputStream bais = new ByteArrayInputStream(receivePacket.getData());
-                DataInputStream dis = new DataInputStream(bais);
+                bais = new ByteArrayInputStream(receivePacket.getData());
+                dis = new DataInputStream(bais);
                 
                 // What happens if you read an integer? Two double values? ***
-                float  first = dis.readFloat(); // alternatives: readFloat(); readInt(); dis.readUTF(); 
-                float second = dis.readFloat();
-                System.out.println("first value: " + first + " second value: " + second);
+                first = dis.readFloat(); // alternatives: readFloat(); readInt(); dis.readUTF(); 
+                second = dis.readFloat();
+                
+                System.out.println("first value: " + first + " second value: " + second + " packet count = " + ++packetCount);
             }
         }
         catch(IOException e)
         {
-            System.out.println("Problem with UdpReceiver, see exception trace:");
-            System.out.println(e);
+            if (udpSocket != null)
+                udpSocket.close();
+            
+            if (dis != null)
+                dis.close();
+            
+            System.err.println("Problem with UdpReceiver, see exception trace:");
+            System.err.println(e);
         }
     }
 }
diff --git a/examples/src/UdpMulticastHttpExamples/UdpSender.java b/examples/src/UdpMulticastHttpExamples/UdpSender.java
index 18afda921b..db8b923a65 100644
--- a/examples/src/UdpMulticastHttpExamples/UdpSender.java
+++ b/examples/src/UdpMulticastHttpExamples/UdpSender.java
@@ -21,22 +21,27 @@ public class UdpSender
     public static final int      RECEIVING_PORT = 1415;
     public static final String DESTINATION_HOST = "localhost";
     
-	@SuppressWarnings("SleepWhileInLoop")
-    public static void main(String[] args) 
+    @SuppressWarnings("SleepWhileInLoop")
+    public static void main(String[] args) throws IOException 
     {
+        DatagramSocket udpSocket = null;
+        DataOutputStream dos = null;
+        
         try
         {
             System.out.println("UdpSender started...");
+            
             // Create a UDP socket
-            DatagramSocket udpSocket = new DatagramSocket(SENDING_PORT);
+            udpSocket = new DatagramSocket(SENDING_PORT);
             
             // 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);
-			// alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
+            dos = new DataOutputStream(baos);
+			
+            // alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\"");
             dos.writeFloat(17.0f);
             dos.writeFloat(24.0f);
             byte[] buffer = baos.toByteArray();
@@ -58,14 +63,20 @@ public class UdpSender
             {
                udpSocket.send(packet);
                Thread.sleep(1000); // Send 100, one per second
-               System.out.println("Bert Sent packet " + index + " of 100");
+               System.out.println("Bert sent packet " + index + " of 100");
             }
             System.out.println("UdpSender complete.");
         }
         catch (IOException | InterruptedException e)
         {
-            System.out.println("Problem with UdpSender, see exception trace:");
-            System.out.println(e);
+            if (udpSocket != null)
+                udpSocket.close();
+            
+            if (dos != null)
+                dos.close();
+            
+            System.err.println("Problem with UdpSender, see exception trace:");
+            System.err.println(e);
         }
     }
 }
-- 
GitLab