2013-08-18 222 views
2

我想製作一個小程序,爲桌面製作實時流。 它應該是這樣的,你發送圖片到回聲服務器,並將其響應給客戶端。 你會得到圖像。並排。所以它就像一部電影(或類似的東西)。 但我總是得到一個indexoutofboundsexception。錯誤在哪裏,或者我該如何改進我的程序。indexoutofboundsexception通過套接字發送圖片

的ImageIO.write線thows錯誤

//客戶端主

公共類主要{

public static void main(String[] args) { 

    Frame frm = new Frame(); 

    Frame.Client client; 

    frm.setLayout(null); 
    frm.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE); 
    frm.setResizable(false); 
    frm.setSize(1600,900); 
    frm.setVisible(true); 



} 

}

//獲取併發送Desktopimage

public class desktopCapture {

Robot robo; 
BufferedImage screenImage; 
Rectangle bounding; 

public desktopCapture() { 
    try { 
     bounding = new Rectangle(0,0,1600,900); 
     robo = new Robot(); 
    } catch (AWTException e) {e.printStackTrace();} 
} 

public void sendScreenCapture(Socket client) { 
    screenImage = robo.createScreenCapture(bounding); 
    try { 
     ImageIO.write(screenImage, "png", client.getOutputStream()); 

    } catch (IOException e) {e.printStackTrace();} 
} 

}

//在幀中的兩個功能ActionListener對象,所以我可以說誰流了桌面和只得到圖像至。 公共無效readImage(){ 而(真){

  try { 
       while((screenImage = ImageIO.read(in)) != null){ 

        repaintScreen(); 

       } 
      } catch (IOException e) {e.printStackTrace();} 
     } 
    } 

公共無效sendImage(){

 try { 
      while(true){ 
      dC.sendScreenCapture(client); 
      System.out.println("read1"); 
      while((screenImage = ImageIO.read(in)) != null){ 
      System.out.println("read2"); 
      ImageIO.write(screenImage, "png", new File("image1.png")); 

      Thread.sleep(250); 
      } 
      repaintScreen(); 
      screenImage = null; 

      } 
      } catch (IOException | InterruptedException e) {e.printStackTrace();} 

    } 
    } 

} 

//線程的客戶端

公共類處理程序實現Runnable {

Socket client; 
OutputStream out; 
InputStream in; 
PrintWriter writer; 
BufferedImage image; 

public handler(Socket client) { 
    this.client = client; 
} 

@Override 
public void run() { 

    try { 

     in = client.getInputStream();  
    BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
    while(true) { 
     System.out.println("write1"); 
     while((image = ImageIO.read(in)) != null){ 
      System.out.println("write2"); 
    for(int i = 1;i <= Server.connectionArray.size();i++){ 
     Socket TEMP_SOCK = (Socket)Server.connectionArray.get(i-1); 
    out = TEMP_SOCK.getOutputStream(); 
    writer = new PrintWriter(out); 

    ImageIO.write(image, "png", TEMP_SOCK.getOutputStream()); 
    System.out.println("write3"); 
     } 
     image = null; 
    } 
    } 

    } catch (IOException e) {e.printStackTrace();} 
} 

}

+0

哪行代碼拋出異常?你是否用調試器遍歷代碼來確定代碼爲什麼要訪問超出範圍的索引? –

+0

噢..對不起.. ImageIO.write線條很難相信 –

+0

。發佈堆棧跟蹤。 – EJP

回答

0

我想你for循環更改爲:

int count = Server.connectionArray.size() 
int index = count - 1; 
for(int i = 0;i < count; i++){ 
     Socket TEMP_SOCK = (Socket)Server.connectionArray.get(index); 
     out = TEMP_SOCK.getOutputStream(); 
     writer = new PrintWriter(out); 

     ImageIO.write(image, "png", TEMP_SOCK.getOutputStream()); 
     System.out.println("write3"); 
    } 
+0

當我使用這個我得到一個IndexOutOfBoundsException通過套接字TEMP_SOCK =(套接字)Server.connectionArray.get(i); –

+0

@AlexanderKnotek請參閱編輯。 – BlackHatSamurai

+0

這種方式的工作原理現在僅僅是桌面捕捉左側的indexoutofboundsexception。通過我的服務器,問題解決了我的想法。但客戶端現在通過函數desktopCapture來解決問題。在imageIO.write(); –