0

I am trying to create an Serialized ImageData Object (BufferedImage and long) on client side.And deserialize that object at server side. But the problem is that I am not accessing object instances at server side. I don't know whether the problem is in serializing or in deserializing.

Client: thread 1 is creating ImageData object

thread 2: sending object to server

In short I want to transfer ImageData object from client to server. Code is executing successfully, no exception, no error

 public class ImageData implements Serializable {

 static BufferedImage screencapture;
 static long ImageName;

 ImageData() throws HeadlessException, AWTException
 {
     screencapture=new Robot().createScreenCapture(
     new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
     ImageName=System.currentTimeMillis();
 }

 }

Client

abstract class ScreenCapture implements Runnable {
 static BufferedImage screencapture; 
 static ImageData obj;
 static FileOutputStream fileOut;
 static ObjectOutputStream  out;


  public static void main(String args[]) throws
       AWTException, IOException, InterruptedException {

  // Open your connection to a server, at port 1234
  final Socket ClientSocket = new Socket("localhost",1234);

  try{
      //First thread that is Taking screenshot
      Thread TakeScreenShotthread = new Thread () 
      {
          public void run () {
           try {
                    obj= new ImageData();

            } catch (HeadlessException | IOException e) {

                    e.printStackTrace();
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        } 
    };
    //Thread 2 that is Sending Screenshot to server
    Thread sendingScreenShotThread =new Thread () {
          public void run () {
               try {
                            fileOut =new FileOutputStream("ImageInfo.ser");
                             out = new ObjectOutputStream(fileOut);
                             out.writeObject(obj);

                             System.out.printf("Serialized data is saved in /tmp/ImageInfo.ser");

                } catch (IOException e) {
                e.printStackTrace();
                }       
               finally{
                   try {
                    out.close();
                    fileOut.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                }          
          }
        };
        TakeScreenShotthread.start();
        TakeScreenShotthread.sleep(1000);
        sendingScreenShotThread.start();
        sendingScreenShotThread.sleep(1000);


  }finally
  {
       //Closing Clients
                ClientSocket.close();
     }

 }

Server

       public class ServerConnection
{
   static BufferedImage image;
   static long name;
   static OutputStream out;
   static FileInputStream fileIn;


 public static void main(String args[]) throws IOException
 {
        ServerSocket serversock = new ServerSocket(1234);
       Socket clientSocket = null;
        clientSocket = serversock.accept();
       ObjectInputStream in = null; 
       ImageData obj=null;
    try{       
    boolean processing=true;

   while(processing)
     {
   try{            

       fileIn = new FileInputStream("ImageInfo.ser");
              in = new ObjectInputStream(fileIn);
              obj = (ImageData) in.readObject();
              in.close();
              fileIn.close();
             out = new BufferedOutputStream(new FileOutputStream(path+ obj.ImageName + ".jpg"));
            ImageIO.write( obj.screencapture, "jpg", out);
            System.out.println("Image file written successfully");
   } catch (Exception e) {
   }finally {

       fileIn.close();
       out.close();
       processing=false;
      }
    }
  }
 finally{
    clientSocket.close();
    serversock.close();

        }   
  }
 }
Heights
  • 75
  • 10
  • 1
    Can you show your full code, because I am not able to see any Stream connection between Client and Server through which client can pass the image object to server' – AJ. Mar 04 '15 at 05:48
  • I am sure about the successful connection. that's why I post to the point code. Even then if it is required, I will post full code – Heights Mar 04 '15 at 05:52
  • plz explain..How you are sending the file to server? – AJ. Mar 04 '15 at 05:55
  • Where is this part `socket.getOutputStream()` – AJ. Mar 04 '15 at 06:00
  • possible duplicate of [How to serialize an object that includes BufferedImages](http://stackoverflow.com/questions/15058663/how-to-serialize-an-object-that-includes-bufferedimages) – user253751 Mar 04 '15 at 06:06
  • @immibis I already followed the link and googled my problem. But didn't get satisfactory answer. – Heights Mar 04 '15 at 06:11
  • @Heights Is this your first program as a Socket Programming? – AJ. Mar 04 '15 at 06:12
  • Then you might need to be more specific. What is unsatisfactory about the answers to that question? – user253751 Mar 04 '15 at 06:12
  • No But I am beginner in java socket programming – Heights Mar 04 '15 at 06:12
  • 1
    Its really hard to answer your question. I suggest, first try your serialization thing without socket programming. If you get successful then try it through socket programming . – AJ. Mar 04 '15 at 06:16
  • @immibis I made by ImageData instances Transient as mentioned in the link but still no working – Heights Mar 04 '15 at 06:18

0 Answers0