diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/README.md b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3ec21e2b976a5937ab94a80cb4028ccb79441eae --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/README.md @@ -0,0 +1,39 @@ +## Homework 2: Multicast Networking + +Modify this file to describe your project work. + +Typical deliverables include properly packages source, execution log, and screen shots as appropriate. + +References include +* [README.md](../README.md) for this homework project. +* [README.md](../../../../README.md) for course assignments execution instructions. +* [assignments source subdirectories](../../../../../assignments/src) show examples from previous cohorts. + +Questions and innovation are always welcome, good luck! + + + +************************************************** +Project for HW #3 +Team: "Germany" (Stefan and Bernd) +************************************************** + + + +This program(s) do(es) the following: +- send two numbers, a mathematical operator and a port number over VPN to a TCP receiver +- the TCP receiver calculates the result and send it back to the sender over VPN using UDP! +- the UDP receiver shows the result + +The main focus was to send via TCP over a VPN, do something and send the result back via UDP. + +How to run the project: +0. connect both computer with the argon net +1. run UDPResultReceiver on Bernd's computer +2. run TCPNumberReceiverUDPResultSender on Stefan's computer +3. find Stefan's IP within the argon net +4. change the TCP_ARGON_SERVER_IP in class TCPNumberSender to Stefan's IP. +5. run TCPNumberSender + +(you can edit the numbers and run TCPNumberSender multiple times...) + diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/Screenshots.docx b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/Screenshots.docx new file mode 100644 index 0000000000000000000000000000000000000000..ccb72d8a6b6bf96522119ec8837209f50fba9496 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/Screenshots.docx differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberReceiverUDPResultSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberReceiverUDPResultSender.java new file mode 100644 index 0000000000000000000000000000000000000000..14ac299c8a175857436a6989babc2a83a7575896 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberReceiverUDPResultSender.java @@ -0,0 +1,176 @@ +package MV3500Cohort2020JulySeptember.homework3.WeissenbergerGoericke; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * This class will be connected by an TCP sender over VPN (argon), calculating + * a result of an equation and send the result back via UDP over VPN. + * @date 08/17/2020 + * @group Goericke/Weissenberger + */ +public class TCPNumberReceiverUDPResultSender { + + // Change this to the port where the TCP server is listening + private static final int TCP_ARGON_SERVER_PORT = 2317; + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + try { + + // ServerSocket waits for a connection from a client. + System.out.println("TCPNumberReceiver has started..."); + ServerSocket serverSocket = new ServerSocket(TCP_ARGON_SERVER_PORT); + InetAddress remoteAddress; + + // declare the stream and readers + InputStream inputStream; + InputStreamReader inputStreamReader; + BufferedReader bufferedReader; + + // declare needed variables + String clientMessage; + int number1, number2; + String calculation_Method; + int iUDPResultReceivingPort; + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + while (true) { + + // block until connected to a client + try (Socket clientConnectionSocket = serverSocket.accept()) + { + // Now hook everything up (i.e. set up the streams), Java style: + inputStream = clientConnectionSocket.getInputStream(); + inputStreamReader = new InputStreamReader(inputStream); + bufferedReader = new BufferedReader(inputStreamReader); + + // get the date from TCP packet + clientMessage = bufferedReader.readLine(); + number1 = Integer.parseInt(bufferedReader.readLine()); + number2 = Integer.parseInt(bufferedReader.readLine()); + calculation_Method = bufferedReader.readLine(); + iUDPResultReceivingPort = Integer.parseInt(bufferedReader.readLine()); + + // print them out (for debugging) + System.out.println("Message recived: "+clientMessage); + System.out.println("Number 1 recived: "+number1); + System.out.println("Number 2 recived: "+number2); + System.out.println("Calc Method recived: "+calculation_Method); + System.out.println("Send result to port: "+iUDPResultReceivingPort); + // get the sender IP (is used for sending UDP) + remoteAddress = clientConnectionSocket.getInetAddress(); + + System.out.println("Send result to IP: "+remoteAddress.getHostAddress()); + + // try to send the calculated result as a float via UDP... + sendResultViaUDP(calculateResult(number1, number2, calculation_Method), remoteAddress, iUDPResultReceivingPort); + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server networking: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } + + /** + * send the result to Bernd 10x + * @param result + * @param inetAddress Bernd's IP + * @param port Bernd's UDP port number + * @throws IOException + */ + private static void sendResultViaUDP(float result, InetAddress inetAddress, int port) throws IOException{ + DatagramSocket udpSocket = null; + DataOutputStream dos = null; + + try + { + // 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(); + + DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, inetAddress, port); + + for (int index = 1; index <= 10; index++) + { + dos.writeFloat (result); + 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. + + Thread.sleep(1000); // Send packets at rate of one per second + baos.reset(); // clear the output stream after sending + } + dos.close(); + } + 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(); + } + } + + /** + * calculates the result based on given numbers and operator + * @param n1 first number + * @param n2 second number + * @param operator + * @return the result as a float + */ + private static float calculateResult(int n1, int n2, String operator){ + float result = -1; + switch (operator) { + case "+": + result = n1 + n2; + break; + case "-": + result = n1 - n2; + break; + case "*": + result = n1 * n2; + break; + case "/": + result = n1 / n2; + break; + default: + System.err.println(operator +" is not a valid operator!"); + break; + } + return result; + } +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberSender.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberSender.java new file mode 100644 index 0000000000000000000000000000000000000000..b84a10f1b7da60f66fd938f4aafce52f684ba3d8 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPNumberSender.java @@ -0,0 +1,97 @@ + +package MV3500Cohort2020JulySeptember.homework3.WeissenbergerGoericke; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.Socket; + +/** + * This class will connect to an TCP receiver over VPN (argon) and send + * two numbers + * @date 08/17/2020 + * @author Loki + * @group Weissenberger/Goericke + */ +public class TCPNumberSender { + + // Change this to the IP address of the TCP server 10.1.105.10 (127.0.0.1 just for testing) + private static final String TCP_ARGON_SERVER_IP = "10.1.105.10"; + // Change this to the port where the TCP server is listening + private static final int TCP_ARGON_SERVER_PORT = 2317; + // Where the result should be posted (port) + private static final int UDP_ARGON_RECEIVING_PORT = 1415; + + private static final int NUMBER1 = 16; + private static final int NUMBER2 = 2; + private static final String CALCULATION_METHOD = "-"; + + // how many times should the number being sent + private static final int REPETITION = 1; + private static int counter = 0; + + public static void main(String[] args) throws InterruptedException { + + // Local variables + Socket socket; + OutputStream outputStream; + PrintStream printStream; + + try { + while (counter < REPETITION){ + // increment the counter + counter++; + System.out.println("Homwork 3: TcpNumberSender creating socket over VPN..."); + + // We request an IP to connect to "TCP_ARGON_SERVER_IP" over + // the argon VPN and port number at that IP "TCP_ARGON_SERVER_PORT". + // This establishes a connection to that IP in the form of a Socket + // object. + socket = new Socket(TCP_ARGON_SERVER_IP, TCP_ARGON_SERVER_PORT); // locohost? + + + // instanciate the streams: + outputStream = socket.getOutputStream(); + printStream = new PrintStream(outputStream); + // send a first text to server + printStream.println("This is message from client."); + printStream.flush(); + // wait 1 second + Thread.sleep(1000); + + // sending number 1 + printStream.println(NUMBER1); + // sending number 2 + printStream.println(NUMBER2); + // sending calc method + printStream.println(CALCULATION_METHOD); + // sending the port where the result should be send to + printStream.println(UDP_ARGON_RECEIVING_PORT); + printStream.flush(); + // wait 1 second + Thread.sleep(1000); + + System.out.println("=================================================="); + System.out.println(NUMBER1+" and "+NUMBER2+" were sent to server. Calculating method was: "+CALCULATION_METHOD); + + // close TCP socket to server + socket.close(); + } // end while(true) + } + catch (IOException e) + { + System.err.println("Problem with TCPNumberSender networking:"); + System.err.println("Error: " + e); + + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port: "+TCP_ARGON_SERVER_PORT); + } + } + finally // occurs after any other activity when shutting down + { + System.out.println(); + System.out.println("TCPNumberSender exit"); + } + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPReceiverVPNBridge.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPReceiverVPNBridge.java new file mode 100644 index 0000000000000000000000000000000000000000..82307c052367919d375746cdde15f730f08537b1 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/TCPReceiverVPNBridge.java @@ -0,0 +1,99 @@ + +package MV3500Cohort2020JulySeptember.homework3.WeissenbergerGoericke; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * Just for me for testing - not part of Homework #3! + * @author Loki + */ +public class TCPReceiverVPNBridge { + + protected static String server_IP_VPN = "10.1.105.9"; // argon IP + protected static String server_IP_Local = "192.168.188.103"; // Local IP + + private static final int server_Port = 2317 ; + protected static String client_IP ; + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + try { + + + +// ServerSocket waits for a connection from a client. + // Notice that it is outside the loop; ServerSocket + // needs to be made only once. + System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first + + ServerSocket serverSocket = new ServerSocket(server_Port); + OutputStream os; + PrintStream ps; + InetAddress localAddress, remoteAddress; + int localPort, remotePort; + int serverLoopCount = 0; + + // Server is up and waiting (i.e. "blocked" or paused) + // Loop, infinitely, waiting for client connections. + // Stop the program somewhere else. + while (true) { + + // block until connected to a client + try (Socket clientConnectionSocket = serverSocket.accept()) + { + serverLoopCount++; // increment at beginning of loop for reliability + + // Now hook everything up (i.e. set up the streams), Java style: + os = clientConnectionSocket.getOutputStream(); + ps = new PrintStream(os); + ps.println("This is response " + serverLoopCount + " produced by the server."); // this gets sent back to client! + + // Print some information locally about the Socket connection. + // This includes the port and IP numbers on both sides (the socket pair). + localAddress = clientConnectionSocket.getLocalAddress(); + remoteAddress = clientConnectionSocket.getInetAddress(); + localPort = clientConnectionSocket.getLocalPort(); + remotePort = clientConnectionSocket.getPort(); + + System.out.print ("Server loop " + serverLoopCount + ": "); + + // 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 )) + // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 )) + + // Why is the first IP/port the same, while the second set has different ports? + System.out.println("TcpExample3Server socket pair showing host name, address, port:"); + System.out.println(" (( " + + localAddress.getHostName() + "=" + localAddress.getHostAddress() + ", " + localPort + " ), ( " + + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " ))"); + + if ( localAddress.getHostName().equals( localAddress.getHostAddress()) || + remoteAddress.getHostName().equals(remoteAddress.getHostAddress())) + System.out.println(" note HostName matches address if host has no DNS name"); + + // Notice the use of flush() and try w/ resources. Without + // the try w/ resources the Socket object may stay open for + // a while after the client has stopped needing this + // connection. try w/ resources explicitly ends the connection. + ps.flush(); + // like it or not, you're outta here! + } + } + } catch (IOException e) { + System.err.println("Problem with TcpExample3Server networking: " + e); + + // Provide more helpful information to user if exception occurs due to running twice at one time + if (e instanceof java.net.BindException) { + System.err.println("*** Be sure to stop any other running instances of programs using this port!"); + } + } + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/UDPResultReceiver.java b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/UDPResultReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..1a181820810e24c66cfc6465fdb58824fad30e74 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/UDPResultReceiver.java @@ -0,0 +1,71 @@ + +package MV3500Cohort2020JulySeptember.homework3.WeissenbergerGoericke; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; + +/** + * This class will be connected by an UDP sender over VPN (argon) + * @date 08/17/2020 + * @author Loki + * @group Weissenberger/Goericke + */ +public class UDPResultReceiver { + + public static final int RECEIVING_PORT = 1415; + + /** + * @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(UDPResultReceiver.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); + + float result; + + // You need a new receiving packet to read from every packet received + while (true) + { + udpSocket.receive(receivePacket); // blocks until packet is received + + result = dis.readFloat(); + + dis.reset(); // now clear the input stream after reading, in preparation for next loop + + System.out.println("Calculated result: "+result); + + } + } + 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(UDPResultReceiver.class.getName() + " complete."); // all done + } + } + +} diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.jpg b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5fa827094c869ea38ad6d265395843694f0263b8 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.jpg differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.vsdx b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.vsdx new file mode 100644 index 0000000000000000000000000000000000000000..4d2e6878bd82a1d201bc8f4742ff1a67d4cc5f34 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/logic.vsdx differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.jpg b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.jpg new file mode 100644 index 0000000000000000000000000000000000000000..44faf01bb3a35a5d7072c8705529f2ef73deecd7 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.jpg differ diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.vsdx b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.vsdx new file mode 100644 index 0000000000000000000000000000000000000000..c43b2fe02d509af09eead57cd0180a2e5d5f1567 Binary files /dev/null and b/assignments/src/MV3500Cohort2020JulySeptember/homework3/WeissenbergerGoericke/sequence.vsdx differ