大数据Java基础第十八天作业
2016-06-17 01:34
239 查看
第一题:udp传输屏幕广播(低于64k)。 import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.net.DatagramPacket; import java.net.DatagramSocket; class ClientDemo{ public static void main(String[] args) throws Exception{ ClientUI ui = new ClientUI(); new ClientReceive(ui).start(); } } class ClientUI extends JFrame{ private JLabel label; private ImageIcon icon; public ClientUI(){ ini(); } public void ini(){ this.setBounds(0,0,600,400); this.setLayout(null); label = new JLabel(); label.setBounds(0,0,600,400); label.setLayout(null); icon = new ImageIcon("e:/a.png"); label.setIcon(icon); this.add(label); this.setVisible(true); } public void refreshImage(byte[] image_arr){ label.setIcon(new ImageIcon(image_arr)); } } class ClientReceive extends Thread{ private DatagramSocket socket; private ClientUI ui; public ClientReceive(ClientUI ui) throws Exception{ this.ui = ui; socket = new DatagramSocket(8889); } public void run(){ try{ int i = 0; while(true){ byte[] buf = new byte[1024*60]; DatagramPacket packet = new DatagramPacket(buf,buf.length); socket.receive(packet); int length = packet.getLength(); ui.refreshImage(buf); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); } } } import java.net.DatagramSocket; import java.net.DatagramPacket; import java.net.InetAddress; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.awt.Robot; import javax.imageio.ImageIO; import java.io.FileOutputStream; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPOutputStream; class ServerDemo{ public static void main(String[] args) throws Exception{ DatagramSocket socket = new DatagramSocket(8888); int i = 0; while(true){ byte[] buf = null; buf = new byte[1024*60]; Rectangle rect = new Rectangle(0,0,600,400); BufferedImage image = (new Robot()).createScreenCapture(rect); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image,"jpg",baos); byte[] zipData = baos.toByteArray(); DatagramPacket packet = new DatagramPacket(zipData,zipData.length); InetAddress addr = InetAddress.getByName("127.0.0.1"); packet.setAddress(addr); packet.setPort(8889); socket.send(packet); i++; Thread.sleep(1000); System.out.println(i); } } } 第二题:udp传输屏幕广播,当传递数据64k时,进行拆包组合。没有使用压缩功能。 import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.Map; import java.util.HashMap; import java.io.ByteArrayOutputStream; class ClientDemo{ public static void main(String[] args) throws Exception{ ClientUI ui = new ClientUI(); new ClientReceive(ui).start(); } } class ClientUI extends JFrame{ private JLabel label; private ImageIcon icon; public ClientUI(){ ini(); } public void ini(){ this.setSize(1024,768); this.setLocation(200,200); this.setLayout(null); label = new JLabel(); label.setBounds(0,0,1024,768); label.setLayout(null); icon = new ImageIcon("e:/a.jpg"); label.setIcon(icon); this.add(label); this.setVisible(true); } public void refreshImage(byte[] image_arr){ label.setIcon(new ImageIcon(image_arr)); } } class ClientReceive extends Thread{ private DatagramSocket socket; private ClientUI ui; private Map<Long,Map<Integer,FrameUnit>> frames = new HashMap<Long,Map<Integer,FrameUnit>>(); public ClientReceive(ClientUI ui) throws Exception{ this.ui = ui; socket = new DatagramSocket(8889); } public void run(){ try{ byte[] buf = new byte[1024*60]; DatagramPacket packet = new DatagramPacket(buf,buf.length); while(true){ socket.receive(packet); FrameUnit unit = new FrameUnit(packet); Map<Integer,FrameUnit> old = null; if(frames.containsKey(unit.getGid())){ old = frames.get(unit.getGid()); old.put(unit.getIndex(),unit); }else{ old = new HashMap<Integer,FrameUnit>(); old.put(unit.getIndex(),unit); frames.put(unit.getGid(),old); } int count = unit.getCount(); int receive_count = frames.get(unit.getGid()).size(); if(count == receive_count){ byte[] image = ClientUtil.mergeImage(old); ui.refreshImage(image); } } }catch(Exception e){ e.printStackTrace(); } } } class FrameUnit{ private long gid; private int count; private int index; private byte[] rawData; public FrameUnit(DatagramPacket packet){ parsePacket(packet); } public long getGid(){ return gid; } public int getCount(){ return count; } public int getIndex(){ return index; } public byte[] getRawData(){ return rawData; } public void parsePacket(DatagramPacket packet){ byte[] buf = packet.getData(); int length = packet.getLength(); byte[] validData = new byte[length]; System.arraycopy(buf,0,validData,0,length); //zip this.gid = ClientUtil.bytesToLong(validData); // 0-7 this.count = validData[8]; this.index = validData[9]; rawData = new byte[length - 10]; System.arraycopy(validData,10,rawData,0,length - 10); } } class ClientUtil{ //0-7 = 8 byte 0 0 0 0 0 0 0 0 public static long bytesToLong(byte[] bytes){ long i0 = (bytes[0] & 0xffL) << 56; long i1 = (bytes[1] & 0xffL) << 48; long i2 = (bytes[2] & 0xffL) << 40; long i3 = (bytes[3] & 0xffL) << 32; long i4 = (bytes[4] & 0xffL) << 24; long i5 = (bytes[5] & 0xffL) << 16; long i6 = (bytes[6] & 0xffL) << 8; long i7 = (bytes[7] & 0xffL) << 0; return i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7; } public static byte[] longToBytes(long i){ byte bytes[] = new byte[8]; bytes[0] = (byte)(i >> 56); bytes[1] = (byte)(i >> 48); bytes[2] = (byte)(i >> 40); bytes[3] = (byte)(i >> 32); bytes[4] = (byte)(i >> 24); bytes[5] = (byte)(i >> 16); bytes[6] = (byte)(i >> 8); bytes[7] = (byte)(i >> 0); return bytes; } public static byte[] mergeImage(Map<Integer,FrameUnit> units){ try{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); for(int i = 0;i < units.size();i++){ FrameUnit unit = units.get(i); baos.write(unit.getRawData()); } return baos.toByteArray(); }catch(Exception e){ e.printStackTrace(); } return null; } } import java.net.DatagramSocket; import java.net.DatagramPacket; import java.net.InetAddress; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.awt.Robot; import javax.imageio.ImageIO; import java.io.FileOutputStream; import java.io.ByteArrayOutputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipOutputStream; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; class ServerDemo{ public static void main(String[] args) throws Exception{ (new ServerSender()).start(); } } class ServerSender{ public void start(){ try{ DatagramSocket socket = new DatagramSocket(8888); while(true){ byte[] rawData = captureScreen(); List<DatagramPacket> packet_list = splitPacket(rawData,InetAddress.getByName("127.0.0.1"),8889); for(DatagramPacket p : packet_list){ socket.send(p); } } }catch(Exception e){ e.printStackTrace(); } } public static byte[] captureScreen(){ try{ Rectangle rect = new Rectangle(0,0,1024,768); BufferedImage image = (new Robot()).createScreenCapture(rect); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image,"jpg",baos); byte[] bytes = baos.toByteArray(); return bytes; }catch(Exception e){ e.printStackTrace(); } return null; } public static List<DatagramPacket> splitPacket(byte[] rawData,InetAddress addr,int port){ int all_length = rawData.length; int pack_length = 60 * 1024; int count = 0; if(all_length % pack_length == 0){ count = all_length / pack_length; }else{ count = (all_length / pack_length) + 1; } long gid = System.nanoTime(); List<DatagramPacket> packet_list = new ArrayList<DatagramPacket>(); DatagramPacket packet = null; for(int i = 0;i < count;i++){ byte[] pack = null; if((i + 1) != count){ pack = new byte[pack_length + 10]; System.arraycopy(ServerUtil.longToBytes(gid),0,pack,0,8); pack[8] = (byte)count; pack[9] = (byte)i; System.arraycopy(rawData,i * pack_length,pack,10,pack_length); }else{ int remain = rawData.length - (count - 1) * pack_length; pack = new byte[remain + 10]; System.arraycopy(ServerUtil.longToBytes(gid),0,pack,0,8); pack[8] = (byte)count; pack[9] = (byte)i; System.arraycopy(rawData,i * pack_length,pack,10,remain); } packet = new DatagramPacket(pack,pack.length,addr,port); packet_list.add(packet); } return packet_list; } } //util class class ServerUtil{ //0-7 = 8 byte 0 0 0 0 0 0 0 0 public static long bytesToLong(byte[] bytes){ long i0 = (bytes[0] & 0xffL) << 56; long i1 = (bytes[1] & 0xffL) << 48; long i2 = (bytes[2] & 0xffL) << 40; long i3 = (bytes[3] & 0xffL) << 32; long i4 = (bytes[4] & 0xffL) << 24; long i5 = (bytes[5] & 0xffL) << 16; long i6 = (bytes[6] & 0xffL) << 8; long i7 = (bytes[7] & 0xffL) << 0; return i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7; } public static byte[] longToBytes(long i){ byte bytes[] = new byte[8]; bytes[0] = (byte)(i >> 56); bytes[1] = (byte)(i >> 48); bytes[2] = (byte)(i >> 40); bytes[3] = (byte)(i >> 32); bytes[4] = (byte)(i >> 24); bytes[5] = (byte)(i >> 16); bytes[6] = (byte)(i >> 8); bytes[7] = (byte)(i >> 0); return bytes; } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1790128
相关文章推荐
- 大数据Java基础第十六天、第十七天作业
- main函数的第三个参数envp
- Mac笔记本电脑 睡眠失败,硬关机之后,启动不了!还好折腾好了
- mybatis3: Invalid bound statement (not found)
- 浅谈如何才能快速成为数据分析师
- Jetty:Hello World,使用main启动的简单http server
- 寄云科技PaaS测试报告
- 分布式文件系统HDFS 之一
- HDU(简单题目) 1038 Biker's Trip Odometer 1039 Easier Done Than Said?
- LeetCode之11_Container With Most Water
- 高并发和大数据的比喻
- Leetcode 219. Contains Duplicate II
- 年度钜献,108个大数据文档PDF开放下载
- 年度钜献,108个大数据文档PDF开放下载
- paint().paintComponent()方法的区别
- RAID各级别特性
- Thumbnailator图像处理
- 通用工具pair
- 大数据生态之开源工具简介
- wait_for_completion_timeout 超时返回0 的问题