dimanche 1 mars 2015

Recieving multiple images in the form of byte[] array through datainputStream

Ok so for my server progrma im trying to send 15 images, code:



try{
//t1.start();
for(int i =1; i<16; i++){
baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File("myimage"+i+".jpg"));
ImageIO.write(img, "jpg", baos);
baos.flush();
imageInByte=baos.toByteArray();
outToClient.write(imageInByte,0,imageInByte.length);
outToClient.flush();

//System.out.println(Arrays.toString(imageInByte));
//ImageIO.write(img, "jpg", outToClient);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


This seems to be working ok, its the client side im having problems with. The code for receiving the images:



try
{

Socket sock = new Socket("127.0.0.1", 8001);
DataInputStream inFromServer = new DataInputStream(sock.getInputStream());
System.out.println("networking established");
byte[] message = new byte[120947];

while(true){

inFromServer.read(message);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(message));
File outputfile = new File("saved1.jpg");
ImageIO.write(image, "jpg", outputfile);


}

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


Ok so I know the while(true) loop wont work, ive tried alot of alternatives like an if statement to check the datainputstream for bytes etc, but Im having no joy. Does anyone know how to continuously receive the byte[] arrays from the server? Any help would be appreciated.


Aucun commentaire:

Enregistrer un commentaire