diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageHW3Receiver.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageHW3Receiver.java new file mode 100644 index 0000000000000000000000000000000000000000..c2b5b817432fbeb3d50513164f281b0aa4ec1eae --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageHW3Receiver.java @@ -0,0 +1,76 @@ +/* + * 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 java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JLabel; + +/** + * + * @author garrettloeffelman + */ +public class LoeffelmanSeversonUDPImageHW3Receiver { + + public static final int SENDING_PORT = 1414; + public static final int RECEIVING_PORT = 1415; + public static final String DESINATION_HOST = "localhost"; + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + try + { + System.out.println("UdpReceiver started..."); + // Create a UDP socket + DatagramSocket udpSocket = new DatagramSocket(RECEIVING_PORT); + + Image image = null; + + // You need a new receiving packet to read from every packet received + while (true) + { + byte[] receiveBuffer = new byte[100000]; + DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); + udpSocket.receive(receivePacket); + + // Decode the contents by extracting the data from the packet + ByteArrayInputStream bais = new ByteArrayInputStream(receivePacket.getData()); + BufferedImage bimage = ImageIO.read(bais); + + if (!udpSocket.isConnected()) { + System.out.println("Test2"); + image = bimage; + break; + } + + } + JFrame frame = new JFrame(); + frame.setSize(300, 300); + JLabel label = new JLabel(new ImageIcon(image)); + frame.add(label); + frame.setVisible(true); + udpSocket.close(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + catch(IOException e) + { + System.out.println("Problem with UdpReceiver, see exception trace:"); + System.out.println(e); + } + } + +} diff --git a/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageSender.java b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageSender.java new file mode 100644 index 0000000000000000000000000000000000000000..54bb672578cdbcae5a8e4fa37d5d1cf83f47704a --- /dev/null +++ b/deliverables/src/MV3500Cohort2018JulySeptember/homework3/Loeffelman_Severson_Homework3/LoeffelmanSeversonUDPImageSender.java @@ -0,0 +1,80 @@ +/* + * 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 java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.URL; +import javax.imageio.ImageIO; + +/** + * + * @author garrettloeffelman + */ +public class LoeffelmanSeversonUDPImageSender { + + public static final int SENDING_PORT = 1414; + public static final int RECEIVING_PORT = 1415; + public static final String DESTINATION_HOST = "localhost"; + + @SuppressWarnings("SleepWhileInLoop") + public static void main(String[] args) + { + try + { + System.out.println("UdpSender started..."); + // Create a UDP socket + DatagramSocket udpSocket = new DatagramSocket(SENDING_PORT); + + // 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(); + DataOutputStream dos = new DataOutputStream(baos); + // alternatives: writeFloat(17.0f); writeInt(17); writeUTF("\"hello MV3500 no really\""); + URL url1 = new URL("https://cdn1.iconfinder.com/data/icons/cute-monkey-emoticon/595/MONKEY_EMOTICON-05-512.png"); + URL url2 = new URL("https://images.all-free-download.com/images/graphicthumb/yammi_banana_99225.jpg"); + // 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(url1); + ImageIO.write(image, "png", baos); + byte[] buffer = baos.toByteArray(); + + // Put together a packet to send + + // ID of the host we are sending to + InetAddress destinationAddress = InetAddress.getByName(DESTINATION_HOST); + + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, destinationAddress, RECEIVING_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 index = 1; index <= buffer.length; index++) // avoid infinite send loops in code, they can be hard to kill! + { + udpSocket.send(packet); + System.out.println("Sent packet " + index + " of " + buffer.length); + } + System.out.println("UdpSender complete."); + } + catch (IOException e) + { + System.out.println("Problem with UdpSender, see exception trace:"); + System.out.println(e); + } + } + +}