码迷,mamicode.com
首页 > 编程语言 > 详细

java--UDP屏幕广播代码

时间:2016-04-02 18:45:31      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

1.发送端的代码

这里广播的地址只写了一个

 1 package com.udp.broadcast;
 2 
 3 import java.awt.Robot;
 4 import java.awt.image.BufferedImage;
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.OutputStream;
 9 import java.net.DatagramPacket;
10 import java.net.DatagramSocket;
11 import java.net.InetAddress;
12 import java.util.zip.GZIPInputStream;
13 
14 import javax.imageio.ImageIO;
15 
16 import java.awt.AWTException;
17 import java.awt.Rectangle;
18 
19 public class ScreenBroadcastDemo {
20 
21     public static void main(String[] args) throws Exception {
22         DatagramSocket sendSocket = new DatagramSocket(8888);
23         Rectangle screenRect = null;
24         BufferedImage bufImage = null;
25         ByteArrayOutputStream baos = null;
26         int i = 0;
27         while(true){        
28             screenRect = new Rectangle(0, 0, 300, 400);
29             bufImage = new Robot().createScreenCapture(screenRect);
30             baos = new ByteArrayOutputStream();
31             ImageIO.write(bufImage, "jpg", baos);
32             byte[] byteData = baos.toByteArray();
33             DatagramPacket dataPack = new DatagramPacket(byteData, byteData.length);
34             InetAddress iaddr = InetAddress.getByName("localhost");
35             dataPack.setAddress(iaddr);
36             dataPack.setPort(8889);
37             sendSocket.send(dataPack);
38             i++;
39             System.out.println("广播次数" + i);
40         }
41         
42     }
43 
44 }

2. 接收端的代码

2.1 startClient

 1 package com.udp.receiver;
 2 
 3 public class SartClient {
 4 
 5     public static void main(String[] args) {
 6         ClientUI clientUI = new ClientUI();
 7         new ClientReceiverThread(clientUI).start();
 8     }
 9 
10 }

2.2ClientUI

 1 package com.udp.receiver;
 2 
 3 import java.awt.event.WindowAdapter;
 4 import java.awt.event.WindowEvent;
 5 
 6 import javax.swing.ImageIcon;
 7 import javax.swing.JFrame;
 8 import javax.swing.JLabel;
 9 import javax.swing.JTextArea;
10 
11 import com.sun.javafx.sg.prism.web.NGWebView;
12 import com.sun.org.apache.bcel.internal.generic.IndexedInstruction;
13 
14 public class ClientUI extends JFrame {
15     private JLabel label;
16     private ImageIcon imageIcon;
17     public ClientUI(){
18         InitUI();
19 
20     }
21     /*
22      *窗口初始化
23      */
24     public void InitUI(){
25         this.setLayout(null);
26         this.setVisible(true);
27         this.setBounds(0, 0, 500, 600);
28         label = new JLabel();
29         label.setBounds(0, 0, 300, 400);
30         this.add(label);
31         
32 
33         //窗口关闭时,程序退出。两种方法都可以
34         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
35         this.addWindowListener(new WindowAdapter() {
36             public void windowClosing(WindowEvent e ) {
37                 System.exit(-1);
38             }
39         });
40     }
41     public void refreshUI(ImageIcon icon){
42         label.setIcon(icon);
43         this.add(label);
44     }
45 
46 }

2.3ClientReceiverThread

 1 package com.udp.receiver;
 2 
 3 import java.awt.image.BufferedImage;
 4 import java.io.ByteArrayInputStream;
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.net.DatagramPacket;
 9 import java.net.DatagramSocket;
10 import java.net.SocketException;
11 
12 import javax.imageio.ImageIO;
13 import javax.print.attribute.standard.PrinterMessageFromOperator;
14 import javax.swing.ImageIcon;
15 
16 public class ClientReceiverThread extends Thread {
17     private ClientUI ui;
18     public ClientReceiverThread(ClientUI ui){
19         this.ui = ui;
20     }
21     public void run(){
22         byte[] buf = new byte[1024 * 60];
23         DatagramSocket recvSocket = null;
24         byte[] byteRecvData = null;
25         ByteArrayInputStream bais = null;
26         BufferedImage bufImageRecv = null;
27         ImageIcon ImageIcon = null;
28         DatagramPacket dataPack = new DatagramPacket(buf, buf.length);
29         try {
30             recvSocket = new DatagramSocket(8889);
31             while(true){
32                 recvSocket.receive(dataPack);
33                 byteRecvData = dataPack.getData(); 
34                 bais = new ByteArrayInputStream(byteRecvData);
35                 bufImageRecv = ImageIO.read(bais);
36                 ImageIcon = new ImageIcon(bufImageRecv, "Screen");
37                 ui.refreshUI(ImageIcon);
38                 Thread.sleep(10);
39             }
40         } catch (Exception e) {
41             e.printStackTrace();
42         }            
43         
44     }
45 
46 }

 

java--UDP屏幕广播代码

标签:

原文地址:http://www.cnblogs.com/chenghuili/p/5347856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!