2013-07-23 118 views
0

我試圖發送從客戶端捕獲到的圖像到服務器,使用機器人類捕獲圖像並寫入客戶端套接字。在服務器中,我正在讀取緩衝映像並將其寫入服務器本地存儲區。我希望客戶端定期捕獲截圖併發送到server.server,以讀取圖像並存儲在其存儲庫中。通過套接字發送緩存的圖像從客戶端到服務器

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 to 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 to capture client screen"); 

     ImageIO.write(img, "JPG", c.getOutputStream()); 
     System.out.println("Image capture from 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(); 
    } 

    } 


    } 

服務器控制檯

服務器偵聽..........

服務器線程啓動.........

試圖讀取圖片

圖像讀取成功.....

圖像寫入成功......

客戶端控制檯 你好

線程開始........

客戶

去捕獲客戶端屏幕

圖像捕捉來自客戶端的成功...!

你好

線程開始........

客戶

去捕獲客戶端屏幕 你好

線程開始........

客戶端

去ca Pture客戶端屏幕

像這樣重複。此代碼首次完美工作,然後失敗。每次運行它只捕獲一次圖像。我必須定期捕獲和寫入圖像以進行更改。請幫助我

回答

1

您不需要ImageIO作爲服務器端。只是發送和接收字節:

while ((count = in.read(buffer()) > 0) 
{ 
    out.write(buffer, 0, count); 
} 
1

我看到問題出在服務器上。它第一次接受來自客戶端的連接,

Thread ts=new Thread(new ServerThread(serversocket.accept()));Thread ts=new Thread(new ServerThread(serversocket.accept()));

但客戶端只連接一次

Socket client=new Socket("localhost", 6666);

當第一次傳輸完成時,服務器再次停留在接受等待客戶端進行連接,這再也不會發生。因此無論你應該只發佈一個接受並使用插座,每轉移或關閉兩個插座,在客戶端和服務器,並接受/重新連接。

2
Try this 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); 

     } 
And make sure that you close the client socket once the thread(ClientThread.java) is completed may be in finally block or at the end of code. 
相關問題