0

我正在尝试将从客户端捕获的图像发送到服务器,使用机器人类捕获图像并写入客户端套接字。在服务器中,我正在读取缓冲图像并写入服务器本地存储区域。我希望客户端定期捕获屏幕截图并发送到服务器。服务器读取图像并存储在其存储库中。

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

   }

 }

服务器线程.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();
    }  

    }


   }

服务器控制台

服务器监听…………

服务器线程开始......

试图阅读图像

图片读取成功......

图片写入成功......

客户端控制台你好

话题开始了…………

客户

将捕获客户端屏幕

客户成功的图像捕获...!

你好

话题开始了…………

客户

要捕获客户端屏幕 Hello

话题开始了…………

客户

将捕获客户端屏幕

像这样重复。此代码在失败后第一次完美运行。每次运行它只捕获一次图像。我必须进行哪些更改才能定期捕获和写入图像...请帮助我

4

3 回答 3

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.
于 2013-07-23T15:01:44.993 回答
1

我看到问题出在服务器上。它第一次接受来自客户端的连接时,

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

但客户端只连接一次

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

当第一次传输完成时,服务器再次停留在接受状态,等待客户端进行连接,这将不再发生。因此,您应该只发出一个接受并在每次传输时使用该套接字,或者在客户端和服务器上关闭两个套接字,然后再次进行接受/连接。

于 2013-07-23T10:59:04.347 回答
1

你不需要ImageIO这个的服务器端。只需发送和接收字节:

while ((count = in.read(buffer()) > 0)
{
    out.write(buffer, 0, count);
}
于 2013-07-23T10:48:06.760 回答