diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TCPReceiverVPNBridge.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TCPReceiverVPNBridge.java index 43a014be20109e9c245bb84136253d0995e10c67..9c051f935efe94615f9c3102a73ba91f095caeac 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TCPReceiverVPNBridge.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TCPReceiverVPNBridge.java @@ -1,5 +1,5 @@ -package MV3500Cohort2020JulySeptember.homework4.Weissenberger; +package MV3500Cohort2020JulySeptember.homework4.WeissenbergerGoericke; import java.io.IOException; import java.io.OutputStream; diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TcpServerForFinal.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TcpServerForFinal.java index 5e7066c139149084e89bd0cae45237e550476ddf..f7a8082ede0b933c2927c45b470ebe83394c4f9a 100644 --- a/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TcpServerForFinal.java +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/TcpServerForFinal.java @@ -1,4 +1,4 @@ -package MV3500Cohort2020JulySeptember.homework4.Weissenberger; +package MV3500Cohort2020JulySeptember.homework4.WeissenbergerGoericke; import java.io.*; import java.net.*; @@ -24,6 +24,9 @@ import java.net.*; */ public class TcpServerForFinal { + public static final String DEST_INET_AD = "127.0.0.1"; + public static final String LOCAL_WIFI_INET = "192.168.188.106"; + public static void main(String[] args) { try { @@ -33,7 +36,7 @@ public class TcpServerForFinal { System.out.println("TcpExample3Server has started..."); // it helps debugging to put this on console first //ServerSocket serverSocket = new ServerSocket(2317); - InetAddress[] ia = InetAddress.getAllByName("192.168.188.106"); + InetAddress[] ia = InetAddress.getAllByName(LOCAL_WIFI_INET); for (int i = 0;i < ia.length; i++){ System.out.println("---: "+ia[i]); } @@ -78,6 +81,7 @@ public class TcpServerForFinal { System.out.println("TcpExample3Server socket pair showing host name, address, port:"); } + sendResultViaUDP(serverMessage); } } catch (IOException e) { System.err.println("Problem with TcpExample3Server networking: " + e); @@ -88,4 +92,61 @@ public class TcpServerForFinal { } } } + + /** + * send the result to Stefan 2x + * @param result + * @param inetAddress Stefans's IP + * @param port Stefans's UDP port number + * @throws IOException + */ + private static void sendResultViaUDP(String result) 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(); + InetAddress destinationAddress = InetAddress.getByName(DEST_INET_AD); + DatagramPacket datagramPacket = new DatagramPacket(byteArray, byteArray.length, destinationAddress, 1415); + + for (int index = 1; index <= 2; index++) + { + dos.writeUTF (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(); + } + } } diff --git a/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/UDPResultReceiver.java b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/UDPResultReceiver.java new file mode 100644 index 0000000000000000000000000000000000000000..36390f308218cd8d9d9b43b2a0b9b120e5321922 --- /dev/null +++ b/assignments/src/MV3500Cohort2020JulySeptember/homework4/WeissenbergerGoericke/UDPResultReceiver.java @@ -0,0 +1,71 @@ + +package MV3500Cohort2020JulySeptember.homework4.Weissenberger; + +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 09/02/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); + + String received; + + // You need a new receiving packet to read from every packet received + while (true) + { + udpSocket.receive(receivePacket); // blocks until packet is received + + received = dis.readUTF(); + + dis.reset(); // now clear the input stream after reading, in preparation for next loop + + System.out.println("Received from Bernd: "+received); + + } + } + 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 + } + } + +}