Skip to content
Snippets Groups Projects
Commit c27ca22e authored by bweissenbergergy's avatar bweissenbergergy
Browse files

Merge origin/master

parents e46ea904 fd08893a
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2020JulySeptember.homework3.White;
import java.io.*;
import java.net.*;
/**
* An example of receiving UDP packets. Since very often both the
* sender and receiver are on the same host we use different ports
* for each. This prevents collision complaints from the localhost.
*
* 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; // port used by UdpSender, unneeded here
public static final int RECEIVING_PORT = 1415;
public static final String DESINATION_HOST = "localhost";
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = null;
try
{
System.out.println(UdpReceiver.class.getName() + " started...");
// Create a UDP socket
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
byte[] byteArray = new byte[1500];
DatagramPacket receivePacket = new DatagramPacket(byteArray, byteArray.length);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
DataInputStream dis = new DataInputStream(bais);
boolean isEvenParity;
int packetCount = 0;
int firstInt;
float secondFloat;
String thirdString;
String padding;
// You need a new receiving packet to read from every packet received
while (true)
{
packetCount++; // good practice to increment counter at start of loop, when possible
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(); // packetID
secondFloat = dis.readFloat();
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.
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() + "]" +
" 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)
{
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 socket
{
if (udpSocket != null)
udpSocket.close();
System.out.println(UdpReceiver.class.getName() + " complete."); // all done
}
}
}
ant -f C:\\Users\\ctsfi\\Documents\\NetBeansProjects\\MV3500\\NetworkedGraphicsMV3500\\assignments -Dnb.internal.action.name=run.single -Djavac.includes=MV3500Cohort2020JulySeptember/homework3/White/UdpReceiver.java -Drun.class=MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver run-single
init:
Deleting: C:\Users\ctsfi\Documents\NetBeansProjects\MV3500\NetworkedGraphicsMV3500\assignments\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\ctsfi\Documents\NetBeansProjects\MV3500\NetworkedGraphicsMV3500\assignments\build\built-jar.properties
Compiling 1 source file to C:\Users\ctsfi\Documents\NetBeansProjects\MV3500\NetworkedGraphicsMV3500\assignments\build\classes
warning: [options] bootstrap class path not set in conjunction with -source 8
1 warning
compile-single:
run-single:
MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver started...
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=1, second float value=9999.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=1
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=2, second float value=9998.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=2
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=3, second float value=9997.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=3
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=4, second float value=9996.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=4
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=5, second float value=9995.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=5
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=6, second float value=9994.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=6
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=7, second float value=9993.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=7
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=8, second float value=9992.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=8
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=9, second float value=9991.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=9
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=10, second float value=9990.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=10
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=11, second float value=9989.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=11
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=12, second float value=9988.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=12
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=13, second float value=9987.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=13
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=14, second float value=9986.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=14
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=15, second float value=9985.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=15
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=16, second float value=9984.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=16
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=1, second float value=99.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=17
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=17, second float value=9983.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=18
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=2, second float value=98.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=19
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=18, second float value=9982.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=20
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=3, second float value=97.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=21
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=19, second float value=9981.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=22
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=4, second float value=96.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=23
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=20, second float value=9980.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=24
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=5, second float value=95.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=25
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=21, second float value=9979.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=26
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=6, second float value=94.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=27
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=22, second float value=9978.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=28
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=7, second float value=93.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=29
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=23, second float value=9977.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=30
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=8, second float value=92.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=31
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=24, second float value=9976.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=32
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=9, second float value=91.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=33
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=25, second float value=9975.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=34
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=10, second float value=90.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=35
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=26, second float value=9974.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=36
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=11, second float value=89.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=37
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=27, second float value=9973.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=38
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=12, second float value=88.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=39
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=28, second float value=9972.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=40
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=13, second float value=87.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=41
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=29, second float value=9971.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=42
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=14, second float value=86.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=43
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=30, second float value=9970.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=44
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=15, second float value=85.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=45
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=31, second float value=9969.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=46
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=16, second float value=84.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=47
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=32, second float value=9968.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=48
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=17, second float value=83.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=49
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=33, second float value=9967.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=50
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=18, second float value=82.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=51
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=34, second float value=9966.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=52
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=19, second float value=81.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=53
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=35, second float value=9965.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=54
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=20, second float value=80.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=55
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=36, second float value=9964.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=56
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=21, second float value=79.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=57
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=1, second float value=99.0, third String value="We got London on da Track" isPacketIdEvenParity=false, packet counter=58
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=37, second float value=9963.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=59
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=22, second float value=78.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=60
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=2, second float value=98.0, third String value="We got London on da Track" isPacketIdEvenParity=true, packet counter=61
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=38, second float value=9962.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=62
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=23, second float value=77.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=63
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=3, second float value=97.0, third String value="We got London on da Track" isPacketIdEvenParity=false, packet counter=64
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=39, second float value=9961.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=65
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=24, second float value=76.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=66
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=4, second float value=96.0, third String value="We got London on da Track" isPacketIdEvenParity=true, packet counter=67
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=40, second float value=9960.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=68
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=25, second float value=75.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=69
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=5, second float value=95.0, third String value="We got London on da Track" isPacketIdEvenParity=false, packet counter=70
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=41, second float value=9959.0, third String value="42 is the answer" isPacketIdEvenParity=false, packet counter=71
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=26, second float value=74.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=true, packet counter=72
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=6, second float value=94.0, third String value="We got London on da Track" isPacketIdEvenParity=true, packet counter=73
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=42, second float value=9958.0, third String value="42 is the answer" isPacketIdEvenParity=true, packet counter=74
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=27, second float value=73.0, third String value="chris says we need to understand llamas in order to code" isPacketIdEvenParity=false, packet counter=75
[MV3500Cohort2020JulySeptember.homework3.White.UdpReceiver] packetID=7, second float value=93.0, third String value="We got London on da Track" isPacketIdEvenParity=false, packet counter=76
BUILD STOPPED (total time: 59 seconds)
package MV3500Cohort2020JulySeptember.homework3.White;
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.
*
* 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 = "Alex"; // 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 = 1415;
public static final int TOTAL_PACKETS_TO_SEND = 100;
// here is what we need for lab comms
public static final String DESTINATION_HOST = "10.1.105.8"; // localhost 127.0.0.1 or argon 10.1.105.1 or 10.1.105.1 or whatever
@SuppressWarnings("SleepWhileInLoop")
public static void main(String[] args) throws IOException
{
DatagramSocket udpSocket = 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 = MY_NAME + " says Fuck You, Pay Me"; // no really
String padding = new String();
try
{
System.out.println(UdpSender.class.getName() + " shows how to send simple-type values via DataOutputStream");
System.out.println(UdpSender.class.getName() + " started...");
// Create a UDP socket
udpSocket = new DatagramSocket(); // let system assign output port, then SENDING_PORT not needed
// 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(1500); // how many bytes are in buffer? MTU=1500 is good
dos = new DataOutputStream(baos); // wrapper for writing values, connects both streams
// Put together a packet to send
// these types and order of variables must match on sender and receiver
byte[] byteArray = baos.toByteArray();
// ID of the host we are sending to
InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST);
// ID of the host we are sending from
InetAddress sourceAddress = InetAddress.getByName("localhost"); // possibly identical if source not modified
DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, 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).
// 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 <= 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 = TOTAL_PACKETS_TO_SEND - packetID; // countdown
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); // 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.
// System.out.println("udpSocket output port=" + udpSocket.getLocalPort()); // diagnostic tells what port was chosen by system
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 + "\"," + isPacketIdEvenParity +
")" + padding + " as packet #" + index + " of " + TOTAL_PACKETS_TO_SEND);
baos.reset(); // clear the output stream after sending
}
}
catch (IOException | InterruptedException e)
{
System.err.println("Problem with UdpSender, see exception trace:");
System.err.println(e);
}
finally // clean up prior to exit, don't want to leave behind zombies
{
if (udpSocket != null)
udpSocket.close();
if (dos != null)
dos.close();
System.out.println(UdpSender.class.getName() + " complete."); // all done
}
}
}
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