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