写的一个算是远程控制(局域网)的程序吧,客户端发送屏幕到服务端,在服务端可以鼠标控制客户端,偷懒,只有鼠标操作。
问题是:服务端点击鼠标后,客户端反应的时间太长,请各位高人、朋友指点一下问题在哪里
谢谢。
代码有许多地方不规范还请各位见谅。//服务端的程序
package test.telecontrol;
import java.net.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class TeleServer {
 
static ServerSocket server=null;
static int seriorID=01;
static InputStream in=null;
static PrintStream out=null;
static WatchWindow watch=null;
static BufferedImage image=null;
public static void main(String[] args) {
watch=new WatchWindow();
try{
server=new ServerSocket(6662);
System.out.println("[INFO]  监听6662端口... ");
server.setSoTimeout(100000);
System.out.println("[INFO]  等待客户端连接...");
final Socket socket=server.accept();
System.out.println("[INFO]  客户端"+socket.getInetAddress()+" 已连接");
//建立输入输出流
in=socket.getInputStream();
out=new PrintStream(socket.getOutputStream());
}catch(Exception e){
try {
server.close();
System.out.println("[INFO]  服务端关闭");
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(e);
}

//该线程接收客户端发送来的图像并及时显示在label上,同时将鼠标操作发送给客户端
Thread listen=new Thread(){
public void run(){
while(true){
try {
image=ImageIO.read(in);
if(image!=null){
watch.setImage(image);
if(watch.opertion1!=null){
out.println(watch.opertion1);
out.flush();
watch.opertion1=null;
}
}
} catch (Exception e) {
try {
out.close();
in.close();
server.close();
} catch (IOException e1) {}
System.out.println("[INFO]  读写数据出错");
return;
}
}
}
};
//启动线程
listen.start();
}
}//一个简单的界面用于显示客户端图像
class WatchWindow extends JFrame{
JLabel canvas=null;
JScrollPane panel=null;
static String opertion1=null;
public WatchWindow(){
super("window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
canvas=new JLabel();
panel=new JScrollPane(canvas);
getContentPane().add(panel,BorderLayout.CENTER);
panel.setAutoscrolls(true);
setSize(800,600);
//注册监听器
canvas.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
final MouseEvent e1=e;
opertion1="#OPT:"+e1.getX()+","+e1.getY()+","+e1.getButton();
System.out.println(opertion1);
}
});
this.setVisible(true);
}
public void setImage(Image image){
ImageIcon icon=new ImageIcon(image);
canvas.setIcon(icon);
}
}
===============================
//客户端程序
package test.telecontrol;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.io.*;
import javax.imageio.ImageIO;public class TeleClient { static String opr[]=new String[3];
static Robot robot = null;  
static int count=1;
public static void main(String[] args)throws Exception{

final Socket socket=new Socket("192.168.1.103",6662);
System.out.println("[INFO]  连接到192.168.1.103:6662");
//输出流
final OutputStream out=socket.getOutputStream();
//输入流,用于读取服务器端发过来的数据
final InputStream in=socket.getInputStream();
final BufferedReader dis=new BufferedReader(new InputStreamReader(in));
//确定屏幕大小
Toolkit toolkit = Toolkit.getDefaultToolkit(); 
Dimension screenSize = toolkit.getScreenSize(); 
final Rectangle screenRect = new Rectangle(screenSize); 

robot=new Robot();

//该线程负责发送图像到服务器
Thread send=new Thread(){
public void run(){
while(true){
try{
BufferedImage image = robot.createScreenCapture(screenRect);       
ImageIO.write(image, "png", out);  
out.flush();
// System.out.println("[INFO]  第"+ count +"次发送图像到服务器");
}catch(Exception e){
try {
socket.close();
} catch (IOException e1) {}
return;
}
}
}
};

//该线程用于接受服务器端的数据
Thread receive=new Thread(){
public void run(){
while(true){
try{
if(dis.readLine()!=null){
String opertion=dis.readLine();
System.out.println(opertion);
opertion=opertion.substring(5,opertion.length());
opr=opertion.split(",");
execOpt(opr);
}else;
}catch(IOException e3){
System.out.println(e3);
}
}
}
};
//启动线程
send.start();
receive.start();
}

//按照接受到的数据进行鼠标操作
//point[0]:x坐标
//point[1]:y坐标
//point[2]:鼠标按键
public static void execOpt(String[] point){
try {
if(robot==null)
robot= new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.mouseMove(Integer.parseInt(point[0]),Integer.parseInt(point[1]));
if(Integer.parseInt(point[2])==1){
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}else if(Integer.parseInt(point[2])==2){
robot.mousePress(InputEvent.BUTTON2_MASK);
robot.mouseRelease(InputEvent.BUTTON2_MASK);
}else if(Integer.parseInt(point[2])==3){
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
}
}
}