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

improved looping constructs

parent 71e88e6f
No related branches found
No related tags found
No related merge requests found
...@@ -61,8 +61,8 @@ public class MulticastSender { ...@@ -61,8 +61,8 @@ public class MulticastSender {
multicastSocket.joinGroup(group, DisThreadedNetIF.findIpv4Interface()); multicastSocket.joinGroup(group, DisThreadedNetIF.findIpv4Interface());
// You can join multiple groups here // You can join multiple groups here
DatagramPacket packet; byte[] buffer = baos.toByteArray();
byte[] buffer; DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
for (int index = 0; index < LOOPSIZE; index++) for (int index = 0; index < LOOPSIZE; index++)
{ {
...@@ -70,17 +70,16 @@ public class MulticastSender { ...@@ -70,17 +70,16 @@ public class MulticastSender {
if (index < LOOPSIZE - 1) if (index < LOOPSIZE - 1)
{ {
// Put together an updated packet to send // 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.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 = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); packet.setData(buffer);
multicastSocket.send(packet); multicastSocket.send(packet);
System.out.println("Sent multicast packet " + index + " of " + LOOPSIZE); System.out.println("Sent multicast packet " + index + " of " + LOOPSIZE);
baos.reset(); baos.reset();
...@@ -96,13 +95,14 @@ public class MulticastSender { ...@@ -96,13 +95,14 @@ public class MulticastSender {
} }
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) if (multicastSocket != null)
multicastSocket.close(); multicastSocket.close();
dos.close(); dos.close();
System.err.println("Problem with MulticastSender, see exception trace:");
System.err.println(e);
} }
} }
......
...@@ -2,6 +2,7 @@ package UdpMulticastHttpExamples; ...@@ -2,6 +2,7 @@ package UdpMulticastHttpExamples;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.ByteBuffer;
/** /**
* An example of receiving UDP packets. Since very often both the * An example of receiving UDP packets. Since very often both the
...@@ -26,7 +27,6 @@ public class UdpReceiver ...@@ -26,7 +27,6 @@ public class UdpReceiver
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
DataInputStream dis = null;
int packetCount = 0; int packetCount = 0;
try try
...@@ -38,10 +38,9 @@ public class UdpReceiver ...@@ -38,10 +38,9 @@ public class UdpReceiver
udpSocket.setReceiveBufferSize(1500); udpSocket.setReceiveBufferSize(1500);
udpSocket.setBroadcast(false); // we're just receiving here udpSocket.setBroadcast(false); // we're just receiving here
byte[] receiveBuffer = new byte[udpSocket.getReceiveBufferSize()]; ByteBuffer buffer = ByteBuffer.allocate(1500);
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); DatagramPacket receivePacket = new DatagramPacket(buffer.array(), buffer.capacity());
InputStream bais;
float first, second; 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
...@@ -49,27 +48,23 @@ public class UdpReceiver ...@@ -49,27 +48,23 @@ public class UdpReceiver
{ {
udpSocket.receive(receivePacket); 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? *** // What happens if you read an integer? Two double values? ***
first = dis.readFloat(); // alternatives: readFloat(); readInt(); dis.readUTF(); first = buffer.getFloat(); // alternatives: readFloat(); readInt(); dis.readUTF();
second = dis.readFloat(); second = buffer.getFloat();
buffer.clear();
System.out.println("first value: " + first + " second value: " + second + " packet count = " + ++packetCount); System.out.println("first value: " + first + " second value: " + second + " packet count = " + ++packetCount);
} }
} }
catch(IOException e) 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("Problem with UdpReceiver, see exception trace:");
System.err.println(e); System.err.println(e);
} finally {
if (udpSocket != null)
udpSocket.close();
} }
} }
} }
...@@ -16,7 +16,7 @@ import java.net.*; ...@@ -16,7 +16,7 @@ import java.net.*;
*/ */
public class UdpSender 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 SENDING_PORT = 1414;
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";
...@@ -38,7 +38,7 @@ public class UdpSender ...@@ -38,7 +38,7 @@ public class UdpSender
// 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(1500);
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\"");
...@@ -53,7 +53,7 @@ public class UdpSender ...@@ -53,7 +53,7 @@ public class UdpSender
// ID of the host we are sending from // ID of the host we are sending from
InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified 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 // How fast does this go? Does UDP try to slow it down, or does
// this cause network problems? (hint: yes for an unlimited send // this cause network problems? (hint: yes for an unlimited send
...@@ -65,20 +65,20 @@ public class UdpSender ...@@ -65,20 +65,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(MY_NAME + " " + sourceAddress + " sent packet " + index + " of 100"); System.out.println(MY_NAME + ": " + sourceAddress + " sent packet " + index + " of 100");
} }
System.out.println("UdpSender complete."); System.out.println("UdpSender complete.");
} }
catch (IOException | InterruptedException e) catch (IOException | InterruptedException e)
{ {
System.err.println("Problem with UdpSender, see exception trace:");
System.err.println(e);
} finally {
if (udpSocket != null) if (udpSocket != null)
udpSocket.close(); udpSocket.close();
if (dos != null) if (dos != null)
dos.close(); 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