我的應用程序有一個服務器&客戶端,這2 R通過socket.Now連接的服務器將Java的遠程桌面(與Internet工作)
控制client..client的桌面將捕獲屏幕(AWT .Robot)並將其發送到
當過一個事件發生時(鼠標事件或按鍵事件的服務器)。這是工作的罰款&我
能夠通過LAN &讓沒有時間內截屏WAN.But當我連接
服務器&客戶通過互聯網,屏幕截圖得到延遲傳遞,如果事件
發生,那麼更新的屏幕會2個mins..I我保存屏幕捕獲後
的BufferedImage &轉換它的ImageIcon,因爲它是序列化,&其寫入
ObjectOutputStream中,使服務器將捕獲通過ObjectInputStream的圖像..
所以,我怎麼能增加通過互聯網我的應用程序的速度,我不關心
圖像quality..I只需要快速傳輸...
這是客戶端...
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;
/**
* This class is responisble for sending sreenshot every predefined duration
*/
class ScreenSpyer extends Thread {
Socket socket = null;
Robot robot = null; // Used to capture screen
Rectangle rectangle = null; //Used to represent screen dimensions
boolean continueLoop = true; //Used to exit the program
public ScreenSpyer(Socket socket, Robot robot,Rectangle rect) {
this.socket = socket;
this.robot = robot;
rectangle = rect;
start();
}
public void run(){
ObjectOutputStream oos = null; //Used to write an object to the streem
try{
//Prepare ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
/*
* Send screen size to the server in order to calculate correct mouse
* location on the server's panel
*/
oos.writeObject(rectangle);
}catch(IOException ex){
ex.printStackTrace();
}
while(continueLoop){
//Capture screen
BufferedImage image = robot.createScreenCapture(rectangle);
/* I have to wrap BufferedImage with ImageIcon because BufferedImage class
* does not implement Serializable interface
*/
ImageIcon imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
oos.writeObject(imageIcon);
oos.reset(); //Clear ObjectOutputStream cache
System.out.println("New screenshot sent");
} catch (IOException ex) {
ex.printStackTrace();
}
//wait for 100ms to reduce network traffic
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
這是服務器
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
* @author Halim
* ClientScreenReciever is responsible for recieving client screenshot and displaying
* it in the server. Each connected client has a separate object of this class
*/
class ClientScreenReciever extends Thread {
private ObjectInputStream cObjectInputStream = null;
private JPanel cPanel = null;
private boolean continueLoop = true;
public ClientScreenReciever(ObjectInputStream ois, JPanel p) {
cObjectInputStream = ois;
cPanel = p;
//start the thread and thus call the run method
start();
}
public void run(){
try {
//Read screenshots of the client then draw them
while(continueLoop){
//Recieve client screenshot and resize it to the current panel size
ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();
System.out.println("New image recieved");
Image image = imageIcon.getImage();
image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight()
,Image.SCALE_FAST);
//Draw the recieved screenshot
Graphics graphics = cPanel.getGraphics();
graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
}
} catch (IOException ex) {
ex.printStackTrace();
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
}
歡迎來到StackOverflow!當你問你的問題時,右邊有這個方便的**如何格式**框。值得一讀,就像問題區域上方** [?] **那樣[鏈接的頁面](http://stackoverflow.com/editing-help)。 – 2011-04-10 07:55:04