javax.imageio - Sending buffered image over socket from client to server -
i trying send images captured client server,images captured using robot class , writing client socket. in server reading buffered image , writing server local storage area.i want client capture screenshots @ regular interval , send server.server reads images , stores in repository.
public class serverdemo { public static void main(string[] args) { try { serversocket serversocket=new serversocket(6666); system.out.println("server listening.........."); while(true) { thread ts=new thread( new serverthread(serversocket.accept())); ts.start(); system.out.println("server thread started........."); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
serverthread.java
public class serverthread implements runnable { socket s; bufferedimage img = null; string savelocation="d:\\screenshot\\"; public serverthread(socket server) { this.s=server; } @override public void run() { try { system.out.println("trying read image"); img = imageio.read(s.getinputstream()); system.out.println("image reading successful....."); } catch (ioexception e) { system.out.println(e); // todo auto-generated catch block e.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } file save_path=new file(savelocation); save_path.mkdirs(); try { imageio.write(img, "jpg",new file(savelocation+"img-"+system.currenttimemillis()+".jpg")); system.out.println("image writing successful......"); } catch (ioexception e) { // todo auto-generated catch block system.out.println(e); e.printstacktrace(); } } }
clientdemo.java
public class clientdemo { public static void main(string[] args) throws interruptedexception { try { socket client=new socket("localhost", 6666); while(true) { system.out.println("hello"); thread th=new thread(new clientthread(client)); th.start(); system.out.println("thread started........"); th.sleep(1000*60); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
clientthread.java
public class clientthread implements runnable{ socket c; public clientthread(socket client) { this.c=client; } @override public void run() { try { system.out.println("client"); //while(true){ dimension size=toolkit.getdefaulttoolkit().getscreensize(); robot robot=new robot(); bufferedimage img=robot.createscreencapture(new rectangle(size)); system.out.println("going capture client screen"); imageio.write(img, "jpg", c.getoutputstream()); system.out.println("image capture client success...!"); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (awtexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
server console
server listening..........
server thread started.........
trying read image
image reading successful.....
image writing successful......
client console hello
thread started........
client
going capture client screen
image capture client success...!
hello
thread started........
client
going capture client screen hello
thread started........
client
going capture client screen
repeat this.this code works first time after fails.each time runs capture images once.what change have make capture , write images @ regular intervals...please me
try in clientdemo.java while(true) { system.out.println("hello"); socket client=new socket("localhost", 6666); thread th=new thread(new clientthread(client)); th.start(); system.out.println("thread started........"); th.sleep(1000*60); } , make sure close client socket once thread(clientthread.java) completed may in block or @ end of code.
Comments
Post a Comment