⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 远程屏幕监控系统
💻 JAVA
字号:
//服务器主要是接受客户机的指令,然后执行指令(发送桌面和执行本地鼠标和键盘控制指令)
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Server
{
    public static void main(String args[])
    {
         ServerSocket server=null;
         Socket you=null;
         while(true)
    	 {
    		try 
    	    {
    		    server=new ServerSocket(5555);                                                             //一直在侦听,侦听到往下步走
    	    }
    	    catch(IOException e)
    	    {
    		    System.out.println("正在侦听");
    	    }
    	    try 
    	    {
    	    	you=server.accept();                                                                                   //侦听到用套接字接客户端发过来的套接字
    	    	System.out.println("客户的IP地址:"+you.getInetAddress());
    	    }
    	    catch(IOException e1)
    	    {
    	    	System.out.println("正在等待客户");
    	    }
    	    if(you!=null)
    	    {
    	    	new Server_thread(you).start();
    	    	System.out.println("线程启动");
    	    }
    	}
    }	
}
class Server_thread extends Thread                                 //为网络上要求连接的机子开一个线程,然后执行这
{                                                                                                    //台机器发过来的指令
	Socket socket=null;
	DataOutputStream out=null;
	DataInputStream in=null;
	Robot robot;
	Toolkit toolkit;
	Server_thread(Socket socket)
	{
		try 
		{
			robot=new Robot();
		}
		catch(Exception e)
		{
			System.out.println("robot没建立好");
		}
		toolkit=Toolkit.getDefaultToolkit();
		this.socket=socket;
		try 
		{
			out=new DataOutputStream(socket.getOutputStream());
			in=new DataInputStream(socket.getInputStream());
		}
		catch(IOException e)
		{
			System.out.println("连接不成功");
		}
	}
	public void run()                                                                                                       //接收客户机发来的指令
	{
	    byte data[]=new byte[50];
		while(true)
		{
			try 
			{
				in.read(data);
				String str=new String(data,0,20).trim();
				if(str.equalsIgnoreCase("REFRESH"))                                                       //传送桌面指令
				{
					sendScreen();
				}
				else                                                                                                                    //鼠标或键盘控制指令
				{
					String command=new String(data,0,20).trim();
					System.out.print(command);System.out.println("abc");
					
					int x=Integer.parseInt(new String(data,20,10).trim());
					System.out.print(""+x);System.out.println("abc");
					
					int y=Integer.parseInt(new String(data,30,10).trim());
					System.out.print(""+y);System.out.println("abc");
					
					int button=Integer.parseInt(new String(data,40,10).trim());
					System.out.print(""+button);System.out.println("abc");
					if(command.equalsIgnoreCase("MousePressed"))
					{
						robot.mousePress(button);
					}
					else if(command.equalsIgnoreCase("MouseMoved"))
					{
						 robot.mouseMove(x,y);
					}
					else if(command.equalsIgnoreCase("MouseReleased"))
					{
						robot.mouseRelease(button);
					}
					else if(command.equalsIgnoreCase("MouseWheel"))
					{
						robot.mouseWheel(button);
					}
					else if(command.equalsIgnoreCase("KeyPressed"))
					{
						robot.keyPress(x);
					}
					else if(command.equalsIgnoreCase("KeyReleased"))
					{
						robot.keyRelease(x);
					}
				}
			}
			catch(Exception e)
			{
				System.out.println("客户端已断开");
			}
		}
	}
	public void sendScreen()//throws Exception                                                                 //发送桌面图像给客户机
	{
	    //byte data[]="abc".getBytes();
	    /*try
	    {
	        out.write(data);	
	    }
	    catch(IOException e)
	    {
	    	System.out.println("发送不出去");
	    }*/
	    
	    try
	    {
	    	BufferedImage image=robot.createScreenCapture(new Rectangle(toolkit.getScreenSize()));       //压缩图像
	    	JPEGEncodeParam param=JPEGCodec.getDefaultJPEGEncodeParam(image);
	    	param.setQuality(0.3f,false);
	    	ByteArrayOutputStream output=new ByteArrayOutputStream();
	    	JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(output,param);
	    	encoder.encode(image);
	    	byte data[]=output.toByteArray();
        	out.write(data);
        	System.out.println("发送成功");
        }
        catch(Exception e)
        {
        	System.out.println("发送不成功");
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -