diff --git a/examples/src/UdpMulticastExamples/UdpReceiver.java b/examples/src/UdpMulticastExamples/UdpReceiver.java index 987493ecdd9ce1b68d24a8d455660f6c1501ccfc..baa39776e9e91fd985bccaff487ec53613fdb67f 100644 --- a/examples/src/UdpMulticastExamples/UdpReceiver.java +++ b/examples/src/UdpMulticastExamples/UdpReceiver.java @@ -9,7 +9,8 @@ import java.net.*; * for each. This prevents collision complaints from the localhost. * * Start this before launching UdpSender. - * + * + * @see <a href=" https://docs.oracle.com/javase/tutorial/networking/datagrams/index.html"> https://docs.oracle.com/javase/tutorial/networking/datagrams/index.html</a> * @see <a href="https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html">https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html</a> * @see <a href="https://en.wikipedia.org/wiki/User_Datagram_Protocol">https://en.wikipedia.org/wiki/User_Datagram_Protocol</a> * @author mcgredo @@ -17,9 +18,9 @@ import java.net.*; */ public class UdpReceiver { -// private static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here - private static final int RECEIVING_PORT = 1415; - private static final String DESINATION_HOST = "localhost"; +// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here + public static final int RECEIVING_PORT = 1415; // sharable + public static final String DESTINATION_HOST = "localhost"; /** * Program invocation, execution starts here @@ -38,6 +39,7 @@ public class UdpReceiver udpSocket = new DatagramSocket(RECEIVING_PORT); udpSocket.setReceiveBufferSize(1500); // how many bytes are in buffer? MTU=1500 is good udpSocket.setBroadcast(false); // we're just receiving here +// udpSocket.setReuseAddress(true); byte[] byteArray = new byte[1500]; DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length); @@ -58,6 +60,8 @@ public class UdpReceiver packetCount++; // good practice to increment counter at start of loop, when possible udpSocket.receive(receivePacket); // blocks until packet is received + System.out.println("UdpReceiver address/port: " + udpSocket.getInetAddress().getHostAddress() + "/" + RECEIVING_PORT); + // values of interest follow. order and types of what was sent must match what you are reading! firstInt = dis.readInt(); // packetID secondFloat = dis.readFloat(); diff --git a/examples/src/UdpMulticastExamples/UdpSender.java b/examples/src/UdpMulticastExamples/UdpSender.java index e4e9bab1c2267379bc8e33f1cad7fed231894aa2..95f87fd4765e885bbed384f652dbb9b5e5c49137 100644 --- a/examples/src/UdpMulticastExamples/UdpSender.java +++ b/examples/src/UdpMulticastExamples/UdpSender.java @@ -19,13 +19,14 @@ import java.net.*; */ public class UdpSender { - private static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) -// private static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port - private static final int RECEIVING_PORT = 1415; - private static final int TOTAL_PACKETS_TO_SEND = 100; + public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) +// public static final int SENDING_PORT = 1414; // not needed, can let system choose an open local port + public static final int RECEIVING_PORT = UdpReceiver.RECEIVING_PORT; // 1415; ensure consistent + public static final int TOTAL_PACKETS_TO_SEND = 100; + public static final String DESTINATION_HOST = "localhost"; // here is what we need for lab comms - private static final String DESTINATION_HOST = "10.1.105.16"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever +// public static final String DESTINATION_HOST = "10.1.105.16"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever /** * Program invocation, execution starts here @@ -67,6 +68,8 @@ public class UdpSender InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, RECEIVING_PORT); + + System.out.println("UdpSender address/port: " + destinationAddress.getHostAddress() + "/" + RECEIVING_PORT); // Hmmm, how fast does UDP stream go? Does UDP effectively slow packets down, or does // this cause network problems? (hint: yes for an unlimited send rate, unlike TCP). @@ -100,7 +103,7 @@ public class UdpSender " sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity + ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND); baos.reset(); // clear the output stream after sending - } + } // end for loop } catch (IOException | InterruptedException e) {