Skip to content
Snippets Groups Projects
Commit 248f06a4 authored by advan's avatar advan
Browse files

Merge origin/master

parents 84fe69a2 f6214fb3
No related branches found
No related tags found
No related merge requests found
Showing with 174 additions and 65 deletions
# https://git-scm.com/docs/gitignore
**/genfiles.properties
/nbproject/build-impl.xml~
/nbproject/private/
**/build-impl.xml
**/build-impl.xml~
**/private/
/assignments/build/
/assignments/dist/
/assignments/dist/javadoc/
......@@ -11,8 +13,6 @@
/assignments/nbproject/private/
/assignments/pduLog/PduCaptureLog*.dislog
/examples/pduLog/PduCaptureLog*.dislog
/examples/BasicServletDemo/nbproject/private/
/examples/WebsocketGateway/nbproject/private/
/examples/manifest.mf
/examples/build/
/examples/dist/
......@@ -35,36 +35,31 @@
!/specifications/2019-SIW-Presentation-039_CompressedDis.pdf
!/specifications/IeeeDisPduColorFigures.pdf
/specifications/downloads/*.pdf
/examples/WebsocketGateway/build/
/examples/WebsocketGateway/dist/
/examples/BasicServletDemo/dist/
/examples/BasicServletDemo/build/
/examples/DisShooting/build/
/examples/DisShooting/dist/
/examples/DisShooting/nbproject/private/
/examples/DisDemo/nbproject/private/
/examples/DisDemo/build/
/examples/DisDemo/dist/
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/nbproject/private/
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/build/
/examples/nbproject/genfiles.properties
/examples/nbproject/build-impl.xml
assignments/src/MV3500Cohort2022MayJune/homework2/Royer/build/
/archive/MV3302ClassCode/build/
/archive/MV3302ClassCode/dist/
/archive/MV3302ClassCode/nbproject/private/
/examples/otherProjects/BasicServletDemo/build/
/examples/otherProjects/BasicServletDemo/dist/
/examples/otherProjects/BasicServletDemo/nbproject/private/
/examples/otherProjects/DisDemo/build/
/examples/otherProjects/DisDemo/dist/
/examples/otherProjects/DisDemo/nbproject/private/
/examples/otherProjects/DisShooting/build/
/examples/otherProjects/DisShooting/dist/
/examples/otherProjects/DisShooting/nbproject/private/
/examples/otherProjects/WebsocketGateway/build/
/examples/otherProjects/WebsocketGateway/dist/
/examples/otherProjects/WebsocketGateway/nbproject/private/
# unable to fix, retained as .zip
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/build/
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/build/
/assignments/src/MV3500Cohort2022MayJune/homework2/Royer/Homework2/nbproject/private/
/assignments/src/MV3500Cohort2022MayJune/projects/Royer/TwoCranesBerth/nbproject/private/
/assignments/src/MV3500Cohort2022MayJune/projects/Royer/TwoCranesBerth/nbproject/build-impl.xml~
/assignments/src/MV3500Cohort2022MayJune/projects/Royer/TwoCranesBerth/pduLog/
/examples/projects/BasicServletDemo/build/
/examples/projects/BasicServletDemo/dist/
/examples/projects/DisDemo/build/
/examples/projects/BasicServletDemo/nbproject/private/
/examples/projects/DisDemo/nbproject/private/
/examples/projects/DisDemo/dist/
/examples/projects/DisShooting/build/
/examples/projects/DisShooting/nbproject/private/
/examples/projects/WebsocketGateway/build/
/examples/projects/WebsocketGateway/nbproject/private/
/examples/otherProjects/DisShooting/nbproject/private/private.xml
/examples/otherProjects/DisShooting/build/
genfiles.properties
/archive/MV3302ClassCode/nbproject/private/
The nbproject directory holds [Netbeans](https://netbeans.org) project information.
Students don't need to modify anything here.
Students don't need to modify anything in this directory.
......@@ -15,6 +15,7 @@
<word>accessor</word>
<word>autogeneration</word>
<word>classpath</word>
<word>Cygwin</word>
<word>deliverables</word>
<word>docx</word>
<word>https</word>
......
## Homework 1: Unicast Networking
Welcome everybody! This is where your homework goes.
Deliverables:
......@@ -9,3 +8,5 @@ Deliverables:
Please see the [README.md](../../../README.md) in the parent
[assignments](../../../../assignments) directory for detailed instructions.
Code modified as instructed, and tested
package MV3500Cohort2024JulySeptember.homework1.Timberlake;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
*
* @author Jack
*/
public class Client {
public static void main(String[] args) {
DatagramSocket socket = null;
Scanner scanner = new Scanner(System.in);
try {
// Create a socket to communicate with the server
socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost"); // Server address (localhost for testing)
byte[] sendData = new byte[1024]; // Buffer to store data to send
byte[] receiveData = new byte[1024]; // Buffer to store received data
// Get user input
System.out.print("Enter message to send to server: ");
String message = scanner.nextLine(); //Read user input
// Send message to server
//String message = "Hello from client!"; // Message to send
sendData = message.getBytes(); // Convert message to bytes
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9876);
socket.send(sendPacket); // Send packet to server
// Receive response from server
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket); // Blocking call until a packet is received
String responseMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Client received: " + responseMessage); // Print received response
} catch (Exception e) {
e.printStackTrace(); // Print any exceptions that occur
} finally {
if (scanner != null) {
scanner.close(); // Close scanner
}
if (socket != null && !socket.isClosed()) {
socket.close(); // Close the socket to release resources
}
}
}
}
package MV3500Cohort2024JulySeptember.homework1.Timberlake;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
*
* @author Jack
*/
public class Server {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
// Create a socket to listen on port 9876
socket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024]; // Buffer to store incoming data
byte[] sendData = new byte[1024]; // Buffer to store outgoing data
while (true) {
// Receive packet from client
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket); // Blocking call until a packet is received
String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Server received: " + message); // Print received message
// Prepare response to client
InetAddress clientAddress = receivePacket.getAddress(); // Get client's address
int clientPort = receivePacket.getPort(); // Get client's port
String responseMessage = "Hello From Server, I received: " + message; // Response message
sendData = responseMessage.getBytes(); // Convert response message to bytes
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);
socket.send(sendPacket); // Send response packet to client
}
} catch (Exception e) {
e.printStackTrace(); // Print any exceptions that occur
} finally {
if (socket != null && !socket.isClosed()) {
socket.close(); // Close the socket to release resources
}
}
}
}
......@@ -65,8 +65,8 @@ javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=21
javac.target=21
javac.source=22
javac.target=22
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
......
......@@ -39,7 +39,7 @@ file.reference.jetty-servlet.jar=lib/jetty-servlet-9.4.31.v20200723.jar
file.reference.jetty-util.jar=lib/jetty-util-9.4.31.v20200723.jar
file.reference.jetty-webapp.jar=lib/jetty-webapp-9.4.31.v20200723.jar
file.reference.jetty-xml.jar=lib/jetty-xml-9.4.31.v20200723.jar
file.reference.opendis7-full.jar=../../lib/opendis7-full.jar
file.reference.opendis7-full.jar=../../../lib/opendis7-full.jar
file.reference.servlet-api-3.1.jar=lib/servlet-api-3.1.jar
file.reference.slf4j-api-1.7.5.jar=lib/slf4j-api-1.7.5.jar
file.reference.slf4j-jdk14-1.7.30.jar=lib/slf4j-jdk14-1.7.30.jar
......
......@@ -2,11 +2,11 @@
* <p>WebSocket server and 3JS example, supporting the NPS MOVES MV3500 Networked Graphics course.</p>
*
* <p>Additional details are provided in the
* <a href="../../../../../otherProjects/WebsocketGateway/README.txt">README.txt</a> file.</p>
* <a href="../../../../../otherProjects/WebsocketGateway/README.txt" target="_blank">README.txt</a> file.</p>
*
* @see java.lang.Package
* @see <a href="https://stackoverflow.com/questions/22095487/why-is-package-info-java-useful">StackOverflow: why-is-package-info-java-useful</a>
* @see <a href="https://stackoverflow.com/questions/624422/how-do-i-document-packages-in-java">StackOverflow: how-do-i-document-packages-in-java</a>
* @see <a href="https://stackoverflow.com/questions/22095487/why-is-package-info-java-useful" target="_blank">StackOverflow: why-is-package-info-java-useful</a>
* @see <a href="https://stackoverflow.com/questions/624422/how-do-i-document-packages-in-java" target="_blank">StackOverflow: how-do-i-document-packages-in-java</a>
*/
package edu.nps.moves.gateway;
......@@ -63,7 +63,7 @@ public class TcpExample2ConnectionCounting
try
{
// welcome aboard messages
System.out.println("TcpExample2ConnectionCounting has started and is waiting for a connection.");
System.out.println(TcpExample2ConnectionCounting.class.getName() + " has started and is waiting for a connection.");
System.out.println(" help: https://savage.nps.edu/Savage/developers.html#telnet");
System.out.println(" Windows ipconfig (or Mac ifconfig) indicates current IPv4_Address (for example local wireless IP address)");
System.out.println(" Windows enter (telnet localhost 2317) or Mac enter (nc localhost 2317) for loopback operation, or" );
......@@ -76,26 +76,33 @@ public class TcpExample2ConnectionCounting
int connectionCount = 0; // state variable
ServerSocket serverSocket = new ServerSocket(2317); // server decides here what port to listen on.
// of interest: often client doesn't care what port it uses locally when connecting to that server port.
// of interest: often client doesn't care what port it uses locally when connecting to that server port.
OutputStream outputStream; // can declare these early, but...
PrintStream printStream; // note initial values are null, they still need to be intialized
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
// If you want to stop or pause the program, go somewhere else.
while(true)
{
// serverSocket.accept() blocks! then proceeds once a connection is "accept"ed
try (Socket clientConnection = serverSocket.accept()) { // the accept() method blocks here until a client connects
// note that serverSocket.accept() first blocks! then after waiting, execution proceeds once a connection is "accept"ed
// you can check this behavior by using NetBeans Debugger
try (Socket clientConnectionSocket = serverSocket.accept()) { // the accept() method blocks here until a client connects
connectionCount++; // got another one! a client has connected
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
outputStream = clientConnectionSocket.getOutputStream(); // clientConnectionSocket is new, every time through, so reset the streams
printStream = new PrintStream(outputStream);
// if (connectionCount == 1) // first time through, report connection
// {
// // Where are we? In other words, what is our host number? Advertise it.
// // Note that we use the serverSocket to get address, since host may have multiple network connections.
// // https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
// String localHostAddress = clientConnection.getInetAddress().getHostAddress();
// String localHostAddress = clientConnectionSocket.getInetAddress().getHostAddress();
// System.out.println("Local host address is " + localHostAddress);
// // add diagnostic for host name, note this might only show up for your system's telnet connection since hostnames aren't part of TCP
// String localHostName = clientConnectionSocket.getInetAddress().getHostName();
// System.out.println("Local host name is " + localHostName);
// if (localHostAddress.contains("/"))
// localHostAddress = localHostAddress.substring(localHostAddress.indexOf("/")+1);
// // show localhost IP number to facilitate connections over local area network (LAN, WAN)
......@@ -104,19 +111,19 @@ public class TcpExample2ConnectionCounting
// "(telnet " + localHostAddress + " 2317)..." );
// }
ps.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client
System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
printStream.println("This client response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to remote client
System.out.println("This server response was written by server " + TcpExample2ConnectionCounting.class.getName()); // to server console
ps.println("You were connection #" + connectionCount + ", by my count");
printStream.println("You were connection #" + connectionCount + ", by my count");
// Print some information locally about the Socket connection.
// This includes the port and IP numbers on both sides (the socket pair.)
InetAddress localAddress = clientConnection.getLocalAddress();
InetAddress remoteAddress = clientConnection.getInetAddress();
InetAddress localAddress = clientConnectionSocket.getLocalAddress();
InetAddress remoteAddress = clientConnectionSocket.getInetAddress();
int localPort = clientConnection.getLocalPort();
int remotePort = clientConnection.getPort(); // remember the prior question, why are 2 ports different?
int localPort = clientConnectionSocket.getLocalPort();
int remotePort = clientConnectionSocket.getPort(); // remember the prior question, why are 2 ports different?
// My socket pair connection looks like this, to localhost:
// Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 )) note IPv6
......@@ -125,25 +132,34 @@ public class TcpExample2ConnectionCounting
// Why is first IP/port the same, while the second set has different ports?
System.out.println("Socket pair (server, client): (( " + localAddress.toString() + ", " + localPort + " ), ( " +
remoteAddress.toString() + ", " + remotePort + " ))");
remoteAddress.toString() + ", " + remotePort + " ))");
System.out.println("got another connection, #" + connectionCount); // report progress
// Notice the use of flush() and close(). Without
// the close() to Socket object may stay open for
// a while after the client has stopped needing this
// connection. Close() explicitly ends the connection.
ps.flush();
// Notice the use of flush() to ensure that messages are sent immediately,
// rather than possibly getting buffered for the program's self-perceived efficiency.
outputStream.flush();
// Notice the use of optional close() when complete. Without a close() invocation,
// the clientConnectionSocket object may stay open for a while after the client
// has stopped needing this connection. Close() explicitly ends the connection.
// Try hiding this and see if anything different occurs.
clientConnectionSocket.close();
}
}
}
catch(IOException e)
catch(IOException ioe)
{
System.err.println("Problem with " + TcpExample2ConnectionCounting.class.getName() + " networking:"); // describe what is happening
System.err.println("Error: " + e);
System.err.println("Error: " + ioe);
// Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException)
if (ioe instanceof java.net.BindException)
System.err.println("*** Be sure to stop any other running instances of programs using this port!");
}
finally
{
// given the infinite-loop logic above, this step is "hard to reach" but a good practice nevertheless
System.out.println(TcpExample2ConnectionCounting.class.getName() + " has finished, adios!");
}
}
}
No preview for this file type
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