如何通过Java套接字从客户端向服务器发送多个映像?

时间:2015-02-23 08:00:42

标签: java multithreading sockets

更新

我正在尝试从客户端向服务器发送多个图像。 客户有两个线程

  1. 主题1正在截屏

  2. 主题2正在向服务器发送屏幕截图

  3. 此代码正在拍摄2张截图。但只有第一个屏幕截图成功保存在服务器上。请帮我发送并保存多个图像到服务器。

    没有错误,也不例外。

    输出:

    thread1正在运行......

    thread2正在运行......

    thread1正在运行......

    thread2正在运行......

    客户端

    abstract class ScreenCapture implements Runnable{
    
     static BufferedImage screencapture;
     static ByteArrayOutputStream baos;
     static byte[] ImageInBytes;
    
       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);
    
      final DataOutputStream dos= new DataOutputStream(ClientSocket.getOutputStream());
      DataInputStream in = new DataInputStream(ClientSocket.getInputStream() );
      baos = new ByteArrayOutputStream();
    
      try{
          //First thread that is Taking screenshot
          Thread TakeScreenShotthread = new Thread () 
          {
              public void run () {        
    
              // Capture Screen using BufferedImage Library
               try {
                   screencapture = new Robot().createScreenCapture(
                   new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
                   System.out.println("thread1 is running...");
    
                } catch (HeadlessException e) {
    
                        e.printStackTrace();
                } catch (AWTException e) {
    
                        e.printStackTrace();
                }
            } 
        };
    
        //Thread 2 that is Sending Screenshot to server
        Thread sendingScreenShotThread =new Thread () {
              public void run () {
                  //Sending Screen to Server
                   try {
                          ImageIO.write(screencapture, "jpg", baos);
                          ImageInBytes = baos.toByteArray();
                          dos.write(ImageInBytes);
                         // File Rif = new File(System.currentTimeMillis() + ".jpg");
                          //ImageIO.write(screencapture, "jpg", Rif);
                          System.out.println("thread2 is running...");
    
                    } catch (IOException e) {
                    e.printStackTrace();
                    }       
                   finally{
    
                        try {
                            baos.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }          
              }
            };
            TakeScreenShotthread.start();
            TakeScreenShotthread.sleep(1000);
            sendingScreenShotThread.start();
            sendingScreenShotThread.sleep(1000);
            TakeScreenShotthread.run();
            sendingScreenShotThread.run();
      }finally
      {
           //Closing Clients
                    in.close();
                    baos.close();
                    ClientSocket.close();
      }  
      }
     }
    

    服务器

        public class ServerConnection
        { 
        public static void main(String args[]) throws IOException
        { 
        ServerSocket serversock = new ServerSocket(1234);
        Socket clientSocket = null;
        BufferedReader BF_RecievingGUID;
        clientSocket = serversock.accept();
        InputStream in=clientSocket.getInputStream();
        OutputStream out = null; 
    
       try{    
            boolean processing=true;
           while(processing)
          {
           try {
            byte[] buffer = new byte[1024];
            out = new BufferedOutputStream(new FileOutputStream(path));
    
           while ((in.read(buffer)) >= 0) { 
                 out.write(buffer);
           }
           System.out.println("Image file written successfully");
       } catch (Exception e) {
       }finally {
           processing=false;
            if (out != null) out.close();
         }
       }
      }
      finally{
            BF_RecievingGUID.close();
            out.close();
          clientSocket.close();
            serversock.close();
    
            }   
        }
      }
    

1 个答案:

答案 0 :(得分:0)

它只读取一次,因为你打破了while循环:

if(RecievedImage != null)
{
     ImageIO.write(RecievedImage, "jpg", RecievedImageFile);
     RecievedImage.flush();
     System.out.println("Image file written successfully");
}else{
     System.out.println("image is empty");
}break;  //you break here 

我不明白为什么你这样做:

new Thread(new ThreadHandler(clientSocket)).start();