Skip to content
Snippets Groups Projects
Commit bd8ee028 authored by brutzman's avatar brutzman
Browse files

class demo updates

parent 31baeb04
No related branches found
No related tags found
No related merge requests found
...@@ -11,12 +11,13 @@ import java.net.*; ...@@ -11,12 +11,13 @@ import java.net.*;
* Start this before launching UdpSender. * Start this before launching UdpSender.
* *
* @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html * @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html
* @see https://en.wikipedia.org/wiki/User_Datagram_Protocol
* @author mcgredo * @author mcgredo
* @author brutzman * @author brutzman
*/ */
public class UdpReceiver public class UdpReceiver
{ {
public static final int SENDING_PORT = 1414; // public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here
public static final int RECEIVING_PORT = 1415; public static final int RECEIVING_PORT = 1415;
public static final String DESINATION_HOST = "localhost"; public static final String DESINATION_HOST = "localhost";
...@@ -57,22 +58,23 @@ public class UdpReceiver ...@@ -57,22 +58,23 @@ public class UdpReceiver
udpSocket.receive(receivePacket); // blocks until packet is received udpSocket.receive(receivePacket); // blocks until packet is received
// values of interest follow. order and types of what was sent must match what you are reading! // values of interest follow. order and types of what was sent must match what you are reading!
firstInt = dis.readInt(); firstInt = dis.readInt(); // packetID
secondFloat = dis.readFloat(); secondFloat = dis.readFloat();
thirdString = dis.readUTF(); thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit
isEvenParity = dis.readBoolean(); isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for.
dis.reset(); // clear the input stream after reading
if (isEvenParity) dis.reset(); // now clear the input stream after reading, in preparation for next loop
if (isEvenParity)
padding = " "; padding = " ";
else padding = ""; else padding = "";
System.out.println(UdpReceiver.class.getName() + System.out.println("[" + UdpReceiver.class.getName() + "]" +
", first int value=" + firstInt + " packetID=" + firstInt + // have output message use same name as sender
", second float value=" + secondFloat + ", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character ", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" parity value=" + isEvenParity + padding + " isPacketIdEvenParity=" + isEvenParity + "," + padding +
", packet counter=" + packetCount); " packet counter=" + packetCount);
} }
} }
catch(IOException e) catch(IOException e)
...@@ -80,10 +82,11 @@ public class UdpReceiver ...@@ -80,10 +82,11 @@ public class UdpReceiver
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 // clean up prior to exit, don't want to leave behind zombie finally // clean up prior to exit, don't want to leave behind zombie socket
{ {
if (udpSocket != null) if (udpSocket != null)
udpSocket.close(); udpSocket.close();
System.out.println(UdpReceiver.class.getName() + " complete."); // all done
} }
} }
} }
...@@ -12,22 +12,24 @@ import java.net.*; ...@@ -12,22 +12,24 @@ import java.net.*;
* Start this before launching UdpReceiver. * Start this before launching UdpReceiver.
* *
* @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html * @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html
* @see https://en.wikipedia.org/wiki/User_Datagram_Protocol
* @author mcgredo * @author mcgredo
* @author brutzman * @author brutzman
*/ */
public class UdpSender public class UdpSender
{ {
// System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html // System properties: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8) public static final String MY_NAME = System.getProperty("user.name"); // guru incantation 8)
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"; // localhost 127.0.0.1 or argon 10.1.105.1 public static final int TOTAL_PACKETS_TO_SEND = 100;
public static final String DESTINATION_HOST = "localhost"; // localhost 127.0.0.1 or argon 10.1.105.1
@SuppressWarnings("SleepWhileInLoop") @SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
DatagramSocket udpSocket = null; DatagramSocket udpSocket = null;
DataOutputStream dos = null; DataOutputStream dos = null;
int packetID = 0; // counter variable to send in packet int packetID = 0; // counter variable to send in packet
float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur
String message = "Hello MV3500"; // no really String message = "Hello MV3500"; // no really
...@@ -64,34 +66,33 @@ public class UdpSender ...@@ -64,34 +66,33 @@ public class UdpSender
// How do you know on the receiving side that you haven't received a // How do you know on the receiving side that you haven't received a
// duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility. // duplicate UDP packet, out-of-order packet, or dropped packet? your responsibility.
for (int index = 1; index <= 100; index++) // avoid infinite send loops in code, they can be hard to kill! for (int index = 1; index <= TOTAL_PACKETS_TO_SEND; index++) // avoid infinite send loops in code, they can be hard to kill!
{ {
packetID++; // increment counter, prefer using explicit value to index packetID++; // increment counter, prefer using explicit value to index
value = 100 - packetID; // countdown value = 100 - packetID; // countdown
boolean isEvenParity = ((packetID % 2) == 0); // % is modulo operator boolean isPacketIdEvenParity = ((packetID % 2) == 0); // % is modulo operator; result 0 is even parity, 1 is odd parity
// values of interest follow. order and types of what was sent must match what you are reading! // values of interest follow. order and types of what was sent must match what you are reading!
dos.writeInt (packetID); dos.writeInt (packetID);
dos.writeFloat (value); dos.writeFloat (value);
dos.writeUTF (message); dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit
dos.writeBoolean(isEvenParity); dos.writeBoolean(isPacketIdEvenParity);
dos.flush(); // sends DataOutputStream to ByteArrayOutputStream dos.flush(); // sends DataOutputStream to ByteArrayOutputStream
byteArray = baos.toByteArray(); // OK so go get the flushed result... byteArray = baos.toByteArray(); // OK so go get the flushed result...
datagramPacket.setData(byteArray); // and put it in the packet... datagramPacket.setData(byteArray); // and put it in the packet...
udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking. udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking.
if (isEvenParity) if (isPacketIdEvenParity)
padding = " "; padding = " ";
else padding = ""; else padding = "";
Thread.sleep(1000); // Send packets at rate of one per second Thread.sleep(1000); // Send packets at rate of one per second
System.out.println(UdpSender.class.getName() + ": " + MY_NAME + " " + sourceAddress + System.out.println("[" + UdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + ", " + value + ", \"" + message + "\", " + isEvenParity + " sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of 100"); ")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending baos.reset(); // clear the output stream after sending
} }
System.out.println(UdpSender.class.getName() + " complete."); // all done
} }
catch (IOException | InterruptedException e) catch (IOException | InterruptedException e)
{ {
...@@ -105,6 +106,7 @@ public class UdpSender ...@@ -105,6 +106,7 @@ public class UdpSender
if (dos != null) if (dos != null)
dos.close(); dos.close();
System.out.println(UdpSender.class.getName() + " complete."); // all done
} }
} }
} }
This diff is collapsed.
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