diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Jackson_UdpSender.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Jackson_UdpSender.java new file mode 100644 index 0000000000000000000000000000000000000000..be60576ad0c6f0620bc961e653ee92eb7bc6e929 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Jackson_UdpSender.java @@ -0,0 +1,84 @@ +package MV3500Cohort2018JulySeptember.homework3; + +import java.io.*; +import java.net.*; + +/** + * An example of sending UDP packets. The sending and receiving programs + * use different UDP ports; there can be problems getting this to work + * if both the sending and receiving sockets try to use the same port + * on the same host. + * + * @author mcgredo + */ +public class Jackson_UdpSender +{ + + public static final int SENDING_PORT = 1414; + public static final int RECEIVING_PORT = 1415; + public static final String DESTINATION_HOST = "localhost"; + + @SuppressWarnings("SleepWhileInLoop") + + public static void main(String[] args) + { + //int idx = 0; + String[] preamble = new String[10]; + preamble[0] = " We the People of the United States, "; + preamble[1] = " in Order to form a more perfect Union, "; + preamble[2] = " establish Justice, "; + preamble[3] = " insure domestic Tranquility, "; + preamble[4] = " provide for the common defence,"; + preamble[5] = " promote the general Welfare, "; + preamble[6] = " and secure the Blessings of Liberty "; + preamble[7] = " to ourselves and our Posterity, "; + preamble[8] = " do ordain and establish this Constitution "; + preamble[9] = " for the United States of America."; + try + { + System.out.println("UdpSender started..."); + // Create a UDP socket + DatagramSocket 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\""); + + + + // Put together a packet to send + + // ID of the host we are sending to + InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST); + + //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 + // rate, unlike TCP). How do you know on the receiving side + // that you haven't received a duplicate UDP packet, out of + // order packet, or dropped packet? + + for (int index = 1; index <= 10; index++) // avoid infinite send loops in code, they can be hard to kill! + { + dos.writeChars(preamble[index-1]); + byte[] buffer = baos.toByteArray(); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, destinationAddress, RECEIVING_PORT ); + udpSocket.send(packet); + Thread.sleep(100); // Send 100, one per second + System.out.println("Sent packet " + index + " of 10"); + + } + System.out.println("UdpSender complete."); + } + catch (IOException | InterruptedException e) + { + System.out.println("Problem with UdpSender, see exception trace:"); + System.out.println(e); + } + } +}