TCP/UDP实现远程控制

这里主要应用到的是套接字socket,ServerSocket和机器人Robot
// 初始化服务端的方法
public void initServer() throws IOException {
// 创建服务端套接字对象
ServerSocket server = new ServerSocket(10010);

System.out.println("服务器已经启动,等待连接");

// 等待客户端连接,如果有客户端连接上,会收到客户端套接字
Socket socket = server.accept();
System.out.println("一个客户端连上了"+socket.getRemoteSocketAddress());

// 启动一个线程来给客户端发送服务端桌面
SendThread st = new SendThread(socket);
st.start();

//启动一个线程来接收客户端发来的控制消息
RecieveThread rt = new RecieveThread(socket);
rt.start();


//RecieveThread线程
Robot robot = new Robot();
InputStream ips = socket.getInputStream();
// 接收的是对象流的消息,包装成对象输入流
ObjectInputStream ois = new ObjectInputStream(ips);
while (true) {
// 收和发的顺序要一致
int x = ois.readInt();
int y = ois.readInt();
int btnNum = ois.readInt();
String type = (String) ois.readObject();
int btnMask = InputEvent.BUTTON1_MASK;
if (btnNum == MouseEvent.BUTTON1) {
btnMask = InputEvent.BUTTON1_MASK;
} else if (btnNum == MouseEvent.BUTTON2) {
btnMask = InputEvent.BUTTON2_MASK;
} else if (btnNum == MouseEvent.BUTTON3) {
btnMask = InputEvent.BUTTON3_MASK;
}

if ("pressed".equals(type)) {
// 移送光标到xy
robot.mouseMove(x, y);
robot.mousePress(btnMask);// 按下对应的鼠标按键
} else if ("released".equals(type)) {
// 移送光标到xy
robot.mouseMove(x, y);
robot.mouseRelease(btnMask);
} else if ("moved".equals(type)) {
// 移送光标到xy
robot.mouseMove(x, y);
}


//SendThread线程
//从套接字上获得输出流
OutputStream ops = socket.getOutputStream();
//包装成对象流
ObjectOutputStream oos = new ObjectOutputStream(ops);
// 创建一个机器人对象,用来截屏
Robot robot = new Robot();
while (true) {
// 获得屏幕的分辨率
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension dim = tool.getScreenSize();
// 要截取的起始点
Point p = new Point(0, 0);
// 指定截取的区域
Rectangle rect = new Rectangle(p, dim);
// 开始截屏,获得截取到的图像
BufferedImage img = robot.createScreenCapture(rect);

//由于BufferedImage没有实现序列化,不能写到IO中
//将BufferedImage包装成可写入IO的图像对象
ImageIcon icon = new ImageIcon(img);
//写出对象
oos.writeObject(icon);
oos.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}


----------------------------------------------------------------------
客户端部分

初始化客户端界面的方法
public void initUI() {
this.setTitle("远程控制");
this.setSize(800, 600);
this.setResizable(false);
this.setDefaultCloseOperation(3);
// this.setLayout(new BorderLayout());

// 用来显示远程桌面的面板
panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel);
jsp.setPreferredSize(new Dimension(600, 400));
jsp.setAutoscrolls(true);
this.add(jsp, BorderLayout.CENTER);

JPanel tool = new JPanel();
tool.setPreferredSize(new Dimension(600, 50));
this.add(tool, BorderLayout.SOUTH);

JLabel ipLabel = new JLabel("IP:");
ipField = new JTextField("127.0.0.1", 10);
JLabel portLabel = new JLabel("PORT:");
portField = new JTextField("10010", 8);
JButton btn = new JButton("连接服务器");

tool.add(ipLabel);
tool.add(ipField);
tool.add(portLabel);
tool.add(portField);
tool.add(btn);

this.setVisible(true);

// 给按钮添加监听器
MyListener mlis = new MyListener();
btn.addActionListener(mlis);

// 给面板添加监听器
MyMouseListener mmlis = new MyMouseListener();
panel.addMouseListener(mmlis);
panel.addMouseMotionListener(mmlis);

}

// panel的鼠标监听器
class MyMouseListener implements MouseListener, MouseMotionListener {

public void mousePressed(MouseEvent e) {
// 得到鼠标的位置
int x = e.getX();
int y = e.getY();
// 得到按下的是哪一个键
int btnNum = e.getButton();
String type = "pressed";
if (oos != null) {
try {
oos.writeInt(x);
oos.writeInt(y);
oos.writeInt(btnNum);
oos.writeObject(type);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}

@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 得到按下的是哪一个键
int btnNum = e.getButton();
String type = "released";
if (oos != null) {
try {
oos.writeInt(x);
oos.writeInt(y);
oos.writeInt(btnNum);
oos.writeObject(type);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 得到按下的是哪一个键
int btnNum = e.getButton();
String type = "moved";
if (oos != null) {
try {
oos.writeInt(x);
oos.writeInt(y);
oos.writeInt(btnNum);
oos.writeObject(type);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

// 按钮监听器
class MyListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
// 获得Ip@端口
String ip = ipField.getText();
String port = portField.getText();
int intPort = Integer.parseInt(port);

try {
// 创建客户端套接字,连接服务端
Socket socket = new Socket(ip, intPort);

OutputStream ops = socket.getOutputStream();
// 需要发送int,String,要包装成对象流
oos = new ObjectOutputStream(ops);

// 不停的接受服务端发来的图片
RecieveThread rt = new RecieveThread(socket, panel);
rt.start();

} catch (Exception e1) {
// 弹出错误提示框
JOptionPane.showMessageDialog(null, "网络异常,无法连接服务器!");
e1.printStackTrace();


客户端RecieveThread线程
// 从Socket上获得输入流
InputStream ips = socket.getInputStream();
// 由于接受的是对象流
ObjectInputStream ois = new ObjectInputStream(ips);
while (true) {
// 读取对象,由于发送的是ImageIcon,这里可以强制转型成ImageIcon
Object obj = ois.readObject();
ImageIcon icon = (ImageIcon) obj;
//获得图像大小(即为服务端桌面大小)
int width = icon.getIconWidth();
int height = icon.getIconHeight();
System.out.println(width+"<>"+height);
//修改panel大小为服务端桌面大小
panel.setPreferredSize(new Dimension(width,height));
//获得画布
Graphics g = panel.getGraphics();
g.drawImage(icon.getImage(), 0,0, null);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值