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

如何通过java套接字将多个映像从客户端发送到服务器?,java,multithreading,sockets,Java,Multithreading,Sockets,已更新 我正在尝试将多个图像从客户端发送到服务器。 客户端有两个线程 线程1正在截图 线程2正在向服务器发送屏幕截图 这段代码有两个截图。但只有第一个屏幕截图成功保存在服务器上。请帮助我向服务器发送和保存多个图像 没有错误,没有例外 输出: 线程1正在运行 线程2正在运行 线程1正在运行 线程2正在运行 客户端 abstract class ScreenCapture implements Runnable{ static BufferedImage screencapture; stat

已更新

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

  • 线程1正在截图

  • 线程2正在向服务器发送屏幕截图

  • 这段代码有两个截图。但只有第一个屏幕截图成功保存在服务器上。请帮助我向服务器发送和保存多个图像

    没有错误,没有例外

    输出:

    线程1正在运行

    线程2正在运行

    线程1正在运行

    线程2正在运行

    客户端

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

    它只读取一次,因为您中断了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();
    

    您如何确定一个图像的结束位置和一个新图像的开始位置?如果您自己不这样做,读者很可能会读取第一幅图像中的所有数据。我还要检查您正在校准的库是否没有关闭任何流。我们如何确定一个图像结束,一个新图像开始?请帮助为什么要启动一个线程来处理套接字,然后在下一个语句中执行I/O?这不可能奏效。这会使这些线程等待1秒-不,不会。虽然这里的Sleep()调用不适合使用,但您应该了解,Sleep()只会暂停调用线程,而不管调用的是哪个线程实例。请阅读此问题。感谢您的澄清,更新了我的答案。BuffereImage ReceiveDimage=ImageIO.read(clientSocket.getInputStream())中仍然存在错误。如果没有错误,我们真的无法提供帮助。。。在问题中添加错误