Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
72695a1d
Commit
72695a1d
authored
2 years ago
by
Islas, Sean (Capt)
Browse files
Options
Downloads
Patches
Plain Diff
Delete HW2
parent
a4e92770
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
assignments/src/MV3500Cohort2023MarchJune/homework2/Islas/HW2
+0
-117
0 additions, 117 deletions
...gnments/src/MV3500Cohort2023MarchJune/homework2/Islas/HW2
with
0 additions
and
117 deletions
assignments/src/MV3500Cohort2023MarchJune/homework2/Islas/HW2
deleted
100644 → 0
+
0
−
117
View file @
a4e92770
package MV3500Cohort2023MarchJune.homework2.Islas;
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 <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
* @author brutzman@nps.edu
*/
public class UnicastUdpReceiver
{
/** Default constructor */
public UnicastUdpReceiver()
{
// default constructor
}
// public static final int SENDING_PORT = 1414; // port used by UdpSender, unneeded here
/** socket value of shared interest */
public static final int UDP_PORT = 1415; // sharable
/** socket value of shared interest */
public static final String DESTINATION_HOST = "localhost";
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public static void main(String[] args)
{
DatagramSocket udpSocket = null;
try
{
// System.out.println(UnicastUdpReceiver.class.getName() + " started...");
System.out.println("What is the airspeed velocity of an unladen Swallow?");
// Create a UDP socket
udpSocket = new DatagramSocket(UDP_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);
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
if (packetCount == 1)
{
if (udpSocket.getInetAddress() == null)
System.out.println("UdpReceiver address/port: UDP socket address null (loopback)" + "/" + UDP_PORT);
else System.out.println("UdpReceiver address/port: " + udpSocket.getInetAddress().getHostAddress() + "/" + UDP_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();
// 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 = "";
if (firstInt == 11) {
System.out.println("You may pass");
}
else {
System.out.println("Pull the lever!");
}
// System.out.println("[" + UnicastUdpReceiver.class.getName() + "]" +
// " port=" + UDP_PORT +
// " 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(UnicastUdpReceiver.class.getName() + " complete."); // all done
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment