【问题标题】:Sending buffered image over socket from client to server通过套接字从客户端发送缓冲图像到服务器
【发布时间】:2013-07-23 10:24:38
【问题描述】:

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

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

    }


   }

服务器控制台

服务器监听…………

服务器线程已启动…………

试图读取图像

图片读取成功.....

图像写入成功......

客户端控制台 你好

线程开始......

客户

要捕获客户端屏幕

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

你好

线程开始......

客户

要捕获客户端屏幕 你好

线程开始......

客户

要捕获客户端屏幕

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

【问题讨论】:

    标签: sockets javax.imageio


    【解决方案1】:
    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.
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        但是客户端只连接一次

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

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

        【讨论】:

          猜你喜欢
          • 2019-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-01
          • 2018-11-11
          相关资源
          最近更新 更多