I have a java program that take snapshots of my screen at say 10 pics per second i want to send it to another computer in the same sequence i tried to send images by writng them one by one on the outputstream

OutputStream os=incoming.getOutputStream();
ImageIO.write(img,"jpg",os);

but i am able to recieve just 1 image i think only the first snapshot...................
pls help......

Are you sure you are not overwriting the file by not providing a unique file name for each image? Also, a working sample of the code which showcases your problem might go a long way in getting people to respond to your queries.

here is my server

import java.awt.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;  
import java.awt.Robot;   
import java.awt.image.BufferedImage;  
import javax.swing.JFrame;
import java.net.*;
import javax.imageio.*;
import java.util.*;
import java.io.*;

   
       
  
public class VideoServer extends JFrame 
{ 
      BufferedImage img;  
      Robot robot;  
      Rectangle rect;  
      
      public static void main(String args[]) 
      {
	  
	  new VideoServer();
	  
      }
      
      public VideoServer()
      {  
      //init components  
	  try 
	  {  
	      robot = new Robot();  
	      rect = new Rectangle(200,200,300,300);  
	  } 
	catch (AWTException e) 
	 { 
		  e.printStackTrace();
	 }
  
	  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	  this.setSize(500,500);  
	//  this.setVisible(true);
  
	  takeShots();  
      }
  
      private void takeShots() 
      {  
	  try
	  {
	      ServerSocket s=new ServerSocket(8189);
	      Socket incoming=s.accept();
	      try
	      {
		  OutputStream os=incoming.getOutputStream();
		  for (int i = 0;i<100 ; i++)
		  {  
		      img = robot.createScreenCapture(rect);  
		  //repaint();
	      
		      ImageIO.write(img,"jpg",os);
		      if(img==null)
			  System.out.println("NULL");
		      try
		      {
	  
			  Thread.sleep(50);
  
		      } 
		      catch (InterruptedException e) 
		      {
	  
			  e.printStackTrace();
  
		      }
  
		  }
	      }
	      finally
	      {
		  s.close();
	      }
	  }
	  catch(IOException e1)
	  {
	      
	  }  
      }
  
    /*  public void paintComponent(Graphics g)
      {
  
	  super.paintComponents(g);
  
	  //Graphics2D g2 = (Graphics2D)g;
  
	  //g2.drawImage(img,0,0,this);
  
      }*/
       
 }

here is my client program..........

import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;


public class ImageClient
{
    public static void main(String args[])
    {
       ArrayList list=new ArrayList(100);
       BufferedImage tmp=new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
	try
	{
	    Socket s=new Socket("127.0.0.1",8189);
	    try
	    {
		InputStream in=s.getInputStream();
		for(int i=0;i<100;i++)
		{
		    
		    tmp=ImageIO.read(in);
		    list.add(tmp);
		 
		  
		}
	    }
	    finally
	    {
		s.close();
	    }
	}
	catch(IOException e)
	{
	    e.printStackTrace();
	}
	try
	{
	    for(int i=0;i<100;i++)
	    {  
		tmp=(BufferedImage)list.get(i);
		
		ImageIO.write(tmp,"jpg",new File("/home/gaurav/java programs/Formedimages/created"+i+".jpg"));
	    }
	}
	catch(Exception exp)
	{
	    System.out.println("hello5");
	}
    }
}

> but i am able to recieve just 1 image i think only the first snapshot

...because it seems that the ImageIO. read() ends up reading the same byte representation of the image over and over again since there is no mechanism of moving ahead or skipping to the next image stream in the client loop.

To get around this, fire off multiple requests from the client which ends up serving the byte representation of the image at that point in time. Or you can write out the list object which holds the byte representation of the given image to the socket stream [using ObjectOutputStream ] which would then be deserialized by the client [using ObjectInputStream ] to retrieve the list object. This obviously involves the additional overhead of serialization/deserialization and makes heavy use of ImageIO.read / ImageIO.write .

Also, to serve multiple requests, you need to modify the server implementation to continuously wait for client requests and process each request in a separate thread.

Also, to serve multiple requests, you need to modify the server implementation to continuously wait for client requests and process each request in a separate thread.

i have not much idea about multithreading but i can work that out...........but how to wait continuosly for client request..................... I thought that once the connection is made client takes whatever server sends to it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.