java做一个远程桌面程序

这是一个远程桌面程序,只有看没有操作。

先写一个服务端,用来发送这台电脑的图像

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.sun.image.codec.jpeg.JPEGCodec;

public class Server extends Thread {
	private Dimension screenSize;
	private Rectangle rectangle;
	private Robot robot;

	public Server() {
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		rectangle = new Rectangle(screenSize);// 可以指定捕获屏幕区域
		try {
			robot = new Robot();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}
	}

	public void run() {
		ZipOutputStream os = null;
		Socket socket = null;
		while (true) {
			try {
				socket = new Socket("192.168.0.222", 5001);// 连接远程IP
				BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕形
																			// 区域
				os = new ZipOutputStream(socket.getOutputStream());// 加入压缩流

				os.setLevel(9);
				os.putNextEntry(new ZipEntry("test.jpg"));
				JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEG
				os.close();
				Thread.sleep(50);// 每秒20帧
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				if (os != null) {
					try {
						os.close();
					}
					catch (Exception ioe) {
					}
				}
				if (socket != null) {
					try {
						socket.close();
					}
					catch (IOException e) {
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		new Server().start();
	}
}

然后写一个客户端,用来接受服务端发来的图像。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipInputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class Client extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public Client() { 
        super(); 
        this.setSize(800, 640); 
        Screen p = new Screen(); 
        Container c = this.getContentPane(); 
        c.setLayout(new BorderLayout()); 
        c.add(p, SwingConstants.CENTER); 
        new Thread(p).start(); 
        SwingUtilities.invokeLater(new Runnable(){ 
            public void run() { 
                setVisible(true); 
            }}); 
    } 
 
    public static void main(String[] args) { 
        new Client(); 
    }
}

class Screen extends JPanel implements Runnable {

	private static final long serialVersionUID = 1L;
	private Image cimage;

	public void run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(5001);// 探听5001端口的连接
			while (true) {
				Socket s = null;
				try {
					s = ss.accept();
					ZipInputStream zis = new ZipInputStream(s.getInputStream());
					zis.getNextEntry();
					cimage = ImageIO.read(zis);// 把ZIP流转换为图片
					repaint();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				finally {
					if (s != null) {
						try {
							s.close();
						}
						catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		catch (Exception e) {
		}
		finally {
			if (ss != null) {
				try {
					ss.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Screen() {
		super();
		this.setLayout(null);
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(cimage, 0, 0, null);
	}
}

最后,两个程序都执行main函数。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值