Skip to content
Snippets Groups Projects
Commit 38270483 authored by Brittokki's avatar Brittokki
Browse files

Merge origin/master

parents 28ace2de bd8ee028
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@
<word>localhost</word>
<word>multicast</word>
<word>Netbeans</word>
<word>netcat</word>
<word>src</word>
<word>TcpExamples</word>
<word>UdpMulticastHttpExamples</word>
......
......@@ -11,12 +11,13 @@ import java.net.*;
* Start this before launching UdpSender.
*
* @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html
* @see https://en.wikipedia.org/wiki/User_Datagram_Protocol
* @author mcgredo
* @author brutzman
*/
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 String DESINATION_HOST = "localhost";
......@@ -57,22 +58,23 @@ public class UdpReceiver
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!
firstInt = dis.readInt();
firstInt = dis.readInt(); // packetID
secondFloat = dis.readFloat();
thirdString = dis.readUTF();
isEvenParity = dis.readBoolean();
dis.reset(); // clear the input stream after reading
thirdString = dis.readUTF(); // string value with guaranteed encoding, matches UTF-8 is 8 bit
isEvenParity = dis.readBoolean(); // ok, we've gotten everything we're looking for.
if (isEvenParity)
dis.reset(); // now clear the input stream after reading, in preparation for next loop
if (isEvenParity)
padding = " ";
else padding = "";
System.out.println(UdpReceiver.class.getName() +
", first int value=" + firstInt +
", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" parity value=" + isEvenParity + padding +
", packet counter=" + packetCount);
System.out.println("[" + UdpReceiver.class.getName() + "]" +
" packetID=" + firstInt + // have output message use same name as sender
", second float value=" + secondFloat +
", third String value=\"" + thirdString + "\"" + // note that /" is literal quote character
" isPacketIdEvenParity=" + isEvenParity + "," + padding +
" packet counter=" + packetCount);
}
}
catch(IOException e)
......@@ -80,10 +82,11 @@ public class UdpReceiver
System.err.println("Problem with UdpReceiver, see exception trace:");
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)
udpSocket.close();
System.out.println(UdpReceiver.class.getName() + " complete."); // all done
}
}
}
......@@ -12,22 +12,24 @@ import java.net.*;
* Start this before launching UdpReceiver.
*
* @see https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html
* @see https://en.wikipedia.org/wiki/User_Datagram_Protocol
* @author mcgredo
* @author brutzman
*/
public class UdpSender
{
// 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 int SENDING_PORT = 1414;
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 String MY_NAME = System.getProperty("user.name"); // guru incantation 8)
public static final int SENDING_PORT = 1414;
public static final int RECEIVING_PORT = 1415;
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")
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
DataOutputStream dos = null;
DataOutputStream dos = null;
int packetID = 0; // counter variable to send in packet
float value = -1.0f; // unreachable value is good sentinel to ensure expected changes occur
String message = "Hello MV3500"; // no really
......@@ -64,34 +66,33 @@ public class UdpSender
// 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.
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
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!
dos.writeInt (packetID);
dos.writeFloat (value);
dos.writeUTF (message);
dos.writeBoolean(isEvenParity);
dos.writeUTF (message); // string value with guaranteed encoding, matches UTF-8 is 8 bit
dos.writeBoolean(isPacketIdEvenParity);
dos.flush(); // sends DataOutputStream to ByteArrayOutputStream
byteArray = baos.toByteArray(); // OK so go get the flushed result...
datagramPacket.setData(byteArray); // and put it in the packet...
udpSocket.send(datagramPacket); // and send it away. boom gone, nonblocking.
if (isEvenParity)
if (isPacketIdEvenParity)
padding = " ";
else padding = "";
Thread.sleep(1000); // Send packets at rate of one per second
System.out.println(UdpSender.class.getName() + ": " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + ", " + value + ", \"" + message + "\", " + isEvenParity +
")" + padding + " as packet #" + index + " of 100");
System.out.println("[" + UdpSender.class.getName() + "] " + MY_NAME + " " + sourceAddress +
" sent values(" + packetID + "," + value + ",\"" + message + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending
}
System.out.println(UdpSender.class.getName() + " complete."); // all done
}
catch (IOException | InterruptedException e)
{
......@@ -105,6 +106,7 @@ public class UdpSender
if (dos != null)
dos.close();
System.out.println(UdpSender.class.getName() + " complete."); // all done
}
}
}
This diff is collapsed.
......@@ -6,11 +6,11 @@
<name>Networked Graphics MV3500</name>
</general-data>
<general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
<!-- Do not use Project Properties customizer when editing this file manually.
To prevent the customizer from showing, create nbproject/project.properties file and enter
auxiliary.show.customizer=false
property there. Adding
auxiliary.show.customizer.message=<message>
<!-- Do not use Project Properties customizer when editing this file manually.
To prevent the customizer from showing, create nbproject/project.properties file and enter
auxiliary.show.customizer=false
property there. Adding
auxiliary.show.customizer.message=<message>
will show your customized message when someone attempts to open the customizer. -->
<name>Networked Graphics MV3500</name>
<properties/>
......
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