diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Receiver.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Receiver.java new file mode 100644 index 0000000000000000000000000000000000000000..e77423089d186a91d34c66527531fc27d1e5f741 --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Receiver.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package MV3500Cohort2018JulySeptember.homework3.Loeffelman_Severson_Homework3; + +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import static java.lang.System.in; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JLabel; + +/** + * + * @author garrettloeffelman + */ +public class LoeffelmanSeversonMulticastImageHw3Receiver { + + + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + public static final int DESTINATION_PORT = 1717; + /** How many routers can be crossed */ + public static final int TTL = 10; + + public static void main(String[] args) + { + try + { + + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + + MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + + int count = 0; + Image image = null; + + while(true) + { + byte[] packetArray = new byte[100000]; + DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length); + + multicastSocket.receive(packet); + count++; + + ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); + BufferedImage bimage = ImageIO.read(bais); + + if(!multicastSocket.isConnected()){ + image = bimage; + break; + } + } + JFrame frame = new JFrame(); + frame.setSize(300, 300); + JLabel label = new JLabel(new ImageIcon(image)); + frame.add(label); + frame.setVisible(true); + + } + catch(Exception e) + { + System.out.println(e); + } + } + +} diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Sender.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Sender.java new file mode 100644 index 0000000000000000000000000000000000000000..edc7c4b04ec128ac2bbbf67538a69a453852bc4b --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonMulticastImageHw3Sender.java @@ -0,0 +1,83 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package MV3500Cohort2018JulySeptember.homework3.Loeffelman_Severson_Homework3; + +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.URL; +import javax.imageio.ImageIO; + +/** + * + * @author garrettloeffelman + */ +public class LoeffelmanSeversonMulticastImageHw3Sender { + + public static final String MULTICAST_ADDRESS = "239.1.2.15"; + public static final int DESTINATION_PORT = 1717; + /** How many routers can be crossed */ + public static final int TTL = 10; + + public static void main(String[] args) + { + try + { + // This is a java/IPv6 problem. You should also add it to the + // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true + // set in the "run" section of preferences. Also, typically + // netbeans must be restarted after these settings. + // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache + System.setProperty("java.net.preferIPv4Stack", "true"); + + + MulticastSocket multicastSocket = new MulticastSocket(1718); + multicastSocket.setTimeToLive(TTL); + InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); + System.out.println(multicastAddress); + // Join group useful on receiving side + multicastSocket.joinGroup(multicastAddress); + // You can join multiple groups here + URL url = new URL("https://cdn1.iconfinder.com/data/icons/cute-monkey-emoticon/595/MONKEY_EMOTICON-05-512.png"); + // 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. + BufferedImage image = ImageIO.read(url); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(image, "png", baos); + byte[] buffer = baos.toByteArray(); + //String encodedImage = Base64.encode(baos.toByteArray()); + // Put together a packet to send + + // muticast group we are sending to--not a single host + + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); + + // How fast does this go? Does UDP try to slow it 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? + + for(int idx = 0; idx < buffer.length; idx++) + { + multicastSocket.send(packet); + System.out.println("Sent multicast packet " + idx); + } + } + catch(Exception e) + { + System.out.println(e); + } + } + +}