Skip to content
Snippets Groups Projects
Commit 46c9efab authored by Terry D. Norbraten's avatar Terry D. Norbraten
Browse files

no object creation in loops

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