我在一個基於套接字的java應用程序工作。它獲取客戶端的屏幕並在服務器上的GUI中顯示它。但問題是它只顯示客戶端的屏幕時間程序已啓動並且不更改它。這裏是我的代碼 服務器端:即時屏幕捕獲Java
try {
img = ImageIO.read(socket.getInputStream());
while(true){
ImageIcon icon = new ImageIcon(img);
label.setIcon(icon);
}
}
catch (IOException e) {}
客戶端:
public class Client{
public static void main(String[] args) throws Exception{
BufferedImage screenShot = new Robot().createScreenCapture(new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "PNG", socket.getOutputStream());
public static void main(String[] args) throws Exception{
Socket socket = new Socket("localhost",1999);
Chat chat = new Chat(socket);
Thread thread = new Thread(chat);
thread.start();
}
}
class Chat implements Runnable{
private Socket socket;
public Chat(Socket socket){
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{ while(true){
BufferedImage screenShot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "PNG", socket.getOutputStream());
}}catch(Exception e){}
}}
錯誤: - 螺紋
例外 「線程3」 java.lang.IndexOutOfBoundsException
at javax.imageio.stream.FileCacheImageOutputStream.seek(Unknown Source)
在javax.imageio.stream.FileCacheImageOutputStream.close(來源不明)
在com.sun.imageio.stream.StreamCloser $ CloseAction.performAction(來源不明)在com.sun.imageio.stream.StreamCloser $ 1.run
(來源不明)
在java.lang.Thread.run(來源不明)
UPDATE:
其實我關閉連接已建立,是造成錯誤甚至在插座。否則尼克的代碼就好了。
你剛纔讀和一次寫的形象呢?循環做! (在服務器上移動imageio.read,在客戶端完成screencapture功能!)。也許在一個定時器5秒 –
你的意思是我shud添加while(true)循環在客戶端文件? – Cybershadow
是的,否則你只上傳一張截圖,因爲代碼只執行一次。在服務器上,你必須更頻繁地讀取套接字(創建一個5秒左右的計時器......)。 –