indexoutofboundsexception通过Socket发送图片

时间:2013-08-18 01:07:14

标签: java indexoutofboundsexception

我想创建一个为Desktop创建实时流的小程序。 它应该是这样您将图片发送到echo-server并将其响应给客户端。 在那里你可以画出图像。并排。所以它就像电影(或类似的东西)。 但我总是得到一个indexoutofboundsexception。错误在哪里或我如何改进我的计划。

ImageIO.write行显示错误

// Client Main

public class Main {

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对象,所以我可以说谁将他的桌面流式传输,哪些只获取图像。 public void readImage(){             而(真){

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

                    repaintScreen();

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

public void 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();}

     }
    }

}

//客户端的线程

public class handler实现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();}
}

}

1 个答案:

答案 0 :(得分: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");
  }