视屏监控(基于TCP/IP一)
程序员文章站
2022-05-15 20:51:09
...
1,编写一个视屏监控软件,远程监控实时画面
初步实现的功能
分析;服务器;截取屏幕 通过对象流写出去
客户端;读取服务器发送过来的数据,并且显示在指定的位置
注意:在写客户端和服务器的时候要边写边测试,不然可能会出很多的问题
代码如下;
服务器入口
package 监控服务器v1;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
*
* 服务器入口
*/
public class MyServer {
public static void main(String[] args) {
//创建服务器套接字
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("等待客户端连接...");
//等待客户端连接
Socket socket = server.accept();
System.out.println(socket.getRemoteSocketAddress()+"连接上服务器");
//获得输出流,包装成对象流
OutputStream ops = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ops);
//启动发送消息的线程
SendThread sendthread = new SendThread(oos);
sendthread.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器发送数据线程
package 监控服务器v1;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ObjectOutputStream;
import javax.swing.ImageIcon;
/**
*
* @author Administrator
*服务器截屏,发送
*/
public class SendThread extends Thread{
private boolean islive = true;
private ObjectOutputStream oos;
public SendThread(ObjectOutputStream oos){
this.oos = oos;
}
public void run(){
while(islive){
try {
//使用Robot截取屏幕
Robot robot = new Robot();
//获得屏幕分辨率
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//截取的矩形
Rectangle rect = new Rectangle(0, 0, dim.width, dim.height);
//保存图片
BufferedImage image = robot.createScreenCapture(rect);
//图片装成ImageIcon来保存
ImageIcon icon = new ImageIcon(image);
//将图片写出去
oos.writeObject(icon);
oos.flush();
Thread.sleep(100);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
客户端入口
package 监控客户端v1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
*
* @author Administrator 客户端界面
*/
public class ClientUI extends JFrame {
JPanel panel = new JPanel();
public static void main(String[] args) {
new ClientUI();
}
private JTextField ipfield;
private JTextField portfield;
// 构造函数
public ClientUI() {
this.setSize(600, 500);
this.setTitle("监控客户端");
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 显示区域中间
JScrollPane jsp = new JScrollPane(panel);
jsp.setAutoscrolls(true);
panel.setBackground(Color.WHITE);
this.add(jsp, BorderLayout.CENTER);
// 按钮区域
JPanel tool = new JPanel();
tool.setBackground(Color.GRAY);
tool.setPreferredSize(new Dimension(1, 80));
this.add(tool, BorderLayout.SOUTH);
// 在按钮区域添加ip和端口的输入框
JLabel ipLabel = new JLabel("ip:");
ipfield = new JTextField("127.0.0.1", 20);
JLabel portLabel = new JLabel("port:");
portfield = new JTextField("8080", 10);
JButton btn = new JButton("连接服务器");
// 将按钮区域的文本,输入框,按钮 添加到按钮区域
tool.add(ipLabel);
tool.add(ipfield);
tool.add(portLabel);
tool.add(portfield);
tool.add(btn);
this.setVisible(true);
// 创建监听器对象给按钮添加监听器
BtnLisener btnlis = new BtnLisener();
btn.addActionListener(btnlis);
}
/**
*
* @author Administrator 添加按钮监听器
*/
class BtnLisener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//获得默认按钮
String command = e.getActionCommand();
//获得事件源
JButton btn = (JButton)e.getSource();
// 获取输入框中的字符串
String ip = ipfield.getText().trim();
String ports = portfield.getText().trim();
// 端口是int型
int port = Integer.parseInt(ports);
if (command.equals("连接服务器")) {
try {
Socket socket = new Socket(ip, port);
btn.setText("连接上服务器");
//创建输入流得到服务器的图片
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
ClientThread th = new ClientThread(ois,panel);
th.start();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
}
客户端接受数据线程
package 监控客户端v1;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.ObjectInputStream;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
*
* @author Administrator 客户端收消息
*/
public class ClientThread extends Thread {
private ObjectInputStream ois;
private JPanel panel;
private boolean islive = true;
public ClientThread(ObjectInputStream ois, JPanel panel) {
this.ois= ois;
this.panel = panel;
}
// 线程收消息
public void run() {
while (islive) {
try {
//读取图片
ImageIcon icon = (ImageIcon)ois.readObject();
//根据读取到的图片设置图片大小
Dimension dim = new Dimension(icon.getIconWidth(),
icon.getIconHeight());
Graphics g = panel.getGraphics();
g.drawImage(icon.getImage(), 0, 0, null);
Thread.sleep(100);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
本程序最好是在两台机器上面测试,本人就是在一台机器上测试的
扩展;实现能打开监控的桌面的软件等等;;;;