java--UDP屏幕广播代码

时间:2023-03-08 21:22:01

1.发送端的代码

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

 package com.udp.broadcast;

 import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.zip.GZIPInputStream; import javax.imageio.ImageIO; import java.awt.AWTException;
import java.awt.Rectangle; public class ScreenBroadcastDemo { public static void main(String[] args) throws Exception {
DatagramSocket sendSocket = new DatagramSocket(8888);
Rectangle screenRect = null;
BufferedImage bufImage = null;
ByteArrayOutputStream baos = null;
int i = 0;
while(true){
screenRect = new Rectangle(0, 0, 300, 400);
bufImage = new Robot().createScreenCapture(screenRect);
baos = new ByteArrayOutputStream();
ImageIO.write(bufImage, "jpg", baos);
byte[] byteData = baos.toByteArray();
DatagramPacket dataPack = new DatagramPacket(byteData, byteData.length);
InetAddress iaddr = InetAddress.getByName("localhost");
dataPack.setAddress(iaddr);
dataPack.setPort(8889);
sendSocket.send(dataPack);
i++;
System.out.println("广播次数" + i);
} } }

2. 接收端的代码

2.1 startClient

 package com.udp.receiver;

 public class SartClient {

     public static void main(String[] args) {
ClientUI clientUI = new ClientUI();
new ClientReceiverThread(clientUI).start();
} }

2.2ClientUI

 package com.udp.receiver;

 import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea; import com.sun.javafx.sg.prism.web.NGWebView;
import com.sun.org.apache.bcel.internal.generic.IndexedInstruction; public class ClientUI extends JFrame {
private JLabel label;
private ImageIcon imageIcon;
public ClientUI(){
InitUI(); }
/*
*窗口初始化
*/
public void InitUI(){
this.setLayout(null);
this.setVisible(true);
this.setBounds(0, 0, 500, 600);
label = new JLabel();
label.setBounds(0, 0, 300, 400);
this.add(label); //窗口关闭时,程序退出。两种方法都可以
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e ) {
System.exit(-1);
}
});
}
public void refreshUI(ImageIcon icon){
label.setIcon(icon);
this.add(label);
} }

2.3ClientReceiverThread

 package com.udp.receiver;

 import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException; import javax.imageio.ImageIO;
import javax.print.attribute.standard.PrinterMessageFromOperator;
import javax.swing.ImageIcon; public class ClientReceiverThread extends Thread {
private ClientUI ui;
public ClientReceiverThread(ClientUI ui){
this.ui = ui;
}
public void run(){
byte[] buf = new byte[1024 * 60];
DatagramSocket recvSocket = null;
byte[] byteRecvData = null;
ByteArrayInputStream bais = null;
BufferedImage bufImageRecv = null;
ImageIcon ImageIcon = null;
DatagramPacket dataPack = new DatagramPacket(buf, buf.length);
try {
recvSocket = new DatagramSocket(8889);
while(true){
recvSocket.receive(dataPack);
byteRecvData = dataPack.getData();
bais = new ByteArrayInputStream(byteRecvData);
bufImageRecv = ImageIO.read(bais);
ImageIcon = new ImageIcon(bufImageRecv, "Screen");
ui.refreshUI(ImageIcon);
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
} } }