📄 client.java
字号:
//客户机主要监视和发送控制指令来达到控制远程服务器
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
class Ipwin extends JFrame //输入服务器端IP地址,然后启动窗口,远程监控被监控的服务器
{
JButton btn;
JTextField txt;
Toolkit toolkit;
public static String ip;
Ipwin(String s)
{
super(s);
toolkit=Toolkit.getDefaultToolkit();
Dimension screen1=toolkit.getScreenSize();
int width=(screen1.width-220)/2;
int height=(screen1.height-90)/2;
setLocation(width,height);
setVisible(true);
btn=new JButton("确定");
txt=new JTextField();
this.getContentPane().add(txt,BorderLayout.CENTER);
this.getContentPane().add(btn,BorderLayout.SOUTH);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ip=txt.getText().trim();
Client client=new Client();
JFrame frame=new JFrame("远程监控窗口");
frame.setSize(1024,768);
frame.setVisible(true);
JScrollPane sc=new JScrollPane(client);
frame.getContentPane().add(sc,BorderLayout.CENTER);
frame.validate();
dispose();
}
});
validate();
}
}
////////////////////////////////////远程监控窗口//////////////////////////////////////////////////////////
public class Client extends JLabel implements Runnable
{
Socket socket=null;
DataOutputStream out=null;
DataInputStream in=null;
Thread thread=null;
Image image;
Client()
{
try
{
InetAddress address=InetAddress.getByName(Ipwin.ip);
System.out.println(Ipwin.ip);
socket=new Socket(address,5555); //找到网络上的服务器//"192.168.0.11"
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
}
catch(IOException e)
{
System.out.println("无法建立连接");
}
addMouseListener(new MouseListener() //在远程监控窗口上安装鼠标监听接口
{
public void mousePressed(MouseEvent e)
{
sendmouse("MousePressed",e.getX(),e.getY(),e.getButton());
}
public void mouseReleased(MouseEvent e)
{
sendmouse("MouseReleased",e.getX(),e.getY(),e.getButton());
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
});
addMouseMotionListener(new MouseMotionListener() //鼠标移动或拖曳监听接口
{
public void mouseDragged(MouseEvent e)
{
sendmouse("MouseMoved",e.getX(),e.getY(),e.getButton());
}
public void mouseMoved(MouseEvent e)
{
sendmouse("MouseMoved",e.getX(),e.getY(),e.getButton());
}
});
addMouseWheelListener(new MouseWheelListener() //鼠标滚轮监听接口
{
public void mouseWheelMoved(MouseWheelEvent e)
{
sendmouse("MouseWheel",e.getX(),e.getY(),e.getUnitsToScroll());
}
});
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() //在远程监控窗口上安装键盘监听接口
{
public void eventDispatched(AWTEvent event)
{
KeyEvent e=(KeyEvent)event;
if (e.getID()==KeyEvent.KEY_PRESSED)
{
sendkey("KeyPressed",e.getKeyCode(),e.getKeyChar(),e.getKeyLocation());
}
else if (e.getID()==KeyEvent.KEY_RELEASED)
{
sendkey("KeyReleased",e.getKeyCode(),e.getKeyChar(),e.getKeyLocation());
}
}
},AWTEvent.KEY_EVENT_MASK);
new Thread(new Runnable() //开一个线程传送“发送桌面”指令给服务器
{
public void run()
{
byte the[]="REFRESH".getBytes();
byte data[]=new byte[20];
for (int i=0;i<20 ;i++ )
{
data[i]=' ';
}
for (int i=0;i<the.length ;i++ )
{
data[i]=the[i];
}
while(true)
{
try
{
out.write(data);
Thread.sleep(1000);
}
catch(Exception e)
{
}
}
}
}).start();
thread=new Thread(this);
thread.start();
}
public void run() //另开一个线程接收服务器传过来的桌面图像
{
byte data[]=new byte[26214400];
while(true)
{
try
{
in.read(data);
ByteArrayInputStream input=new ByteArrayInputStream(data);
getscreen(input);
System.out.println("yes");
}
catch(Exception e)
{
System.out.println("服务器断开");
}
}
}
public void getscreen(InputStream input) //收到服务器的桌面后,调用类中的一个方法,解压并还原图像
{
try
{
JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(input);
BufferedImage image=decoder.decodeAsBufferedImage();
this.image=image;
this.setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
repaint();
}
catch(IOException e)
{
}
}
public void paint(Graphics g) //将还原好的桌面图像在应用程序窗口中画出来
{
g.drawImage(image,0,0,1024,768,this);
}
public void sendkey(String type,int code,char c,int location) //传送键盘控制指令的方法
{
byte the[]=type.getBytes();
byte data[]=new byte[50];
for (int i=0;i<50 ;i++ )
{
data[i]=' ';
}
for (int i=0;i<the.length ;i++ )
{
data[i]=the[i];
}
the=String.valueOf(code).getBytes();
for (int i=0;i<the.length ;i++ )
{
data[20+i]=the[i];
}
the=String.valueOf(Character.getNumericValue(c)).getBytes();
for (int i=0;i<the.length ;i++ )
{
data[30+i]=the[i];
}
the=String.valueOf(location).getBytes();
for (int i=0;i<the.length ;i++ )
{
data[40+i]=the[i];
}
String str=new String(data);
System.out.print(str);System.out.println("abc");
try
{
out.write(data);
}
catch (IOException e)
{
System.out.println("键盘指令发送成功");
}
}
public void sendmouse(String type,int x,int y,int button) //传送鼠标控制指令的方法
{
byte the[]=type.getBytes();
byte data[]=new byte[50];
if (button==MouseEvent.BUTTON1)
{
button=MouseEvent.BUTTON1_MASK;
}
else if (button==MouseEvent.BUTTON3)
{
button=MouseEvent.BUTTON3_MASK;
}
else if (button==MouseEvent.BUTTON2_MASK)
{
button=MouseEvent.BUTTON2_MASK;
}
for (int i=0;i<data.length ;i++ )
{
data[i]=' ';
}
for (int i=0;i<the.length ;i++ )
{
data[i]=the[i];
}
String s=new String(data,0,20);
System.out.print(s);
the=String.valueOf(x).getBytes();
for (int i=0;i<the.length ;i++ )
{
data[20+i]=the[i];
}
String t=new String(data,20,10);
System.out.print(t);
the=String.valueOf(y).getBytes();
for (int i=0;i<the.length ;i++ )
{
data[30+i]=the[i];
}
String r=new String(data,30,10);
System.out.print(r);
the=String.valueOf(button).getBytes();
for (int i=0; i<the.length;i++ )
{
data[40+i]=the[i];
}
String w=new String(data,40,10);
System.out.println(w);
String str=new String(data);
System.out.print(str);System.out.println("abc");
try
{
out.write(data);
}
catch (IOException e)
{
System.out.println("鼠标指令发送成功");
}
}
public static void main(String args[])
{
new Ipwin("输入IP地址");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -