簡體   English   中英

通過Java中的TCP套接字發送圖像

[英]Sending image Through TCP Socket in Java

我正在嘗試制作一個客戶端服務器應用程序,其中服務器列表中的可用圖像名稱和客戶端選擇其中之一進行下載,例如:

服務器中的線程

 public void run()
     {


          try {

            in = new DataInputStream (server.getInputStream());
            out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("To List images write list");
            out.writeUTF("To Exit write 2");
            out.flush();
            while((line = in.readUTF()) != null && !line.equals("2")) {

                if(line.equalsIgnoreCase("list"))
                {
                    String [] images= processor.listImages();
                     for ( int i=0;i<images.length;i++) {
                         out.writeUTF((+1)+"-"+images[i]);
                         out.flush();
                         line = "";
                     }
                     out.writeUTF("-1");
                     line = in.readUTF();

                     if(line.equalsIgnoreCase("2"))
                     {
                         break;
                     }else
                     {
                                        BufferedImage img =processor.processInput(line);

                                        boolean cc = ImageIO.write(img,"JPG",server.getOutputStream());
                         if(!cc)
                         {
                                            out.writeUTF("The entered image is not avaliable !");
                         }else
                         {
                                             System.out.println("Image Sent");
                         }
                     }

                }else if(line.equalsIgnoreCase("2")){
                    break;
            }
            }
                try{
                    in.close();
                    out.close();
                    server.close();
                }catch(Exception e){
                     System.out.println("Error : "+ e.getMessage());
                }

          } catch (IOException e) {
            System.out.println("IOException on socket listen: " + e.getMessage());
          }

     }  

客戶:

public void start() throws IOException
    {
        String line="";

                while(true)
                {
                    try{

                       line = inputFromStream.readUTF(); 
                       System.out.println(line);
                       line = inputFromStream.readUTF(); 
                       System.out.println(line);
                       line = readFromConsol.readLine();

                       writeToStream.writeUTF(line);

                       if(line.equalsIgnoreCase("2")){
                           break;
                       }else if(line.equalsIgnoreCase("list")){
                           boolean check=true;
                           while(check){
                               line = inputFromStream.readUTF();
                               System.out.println(line);
                               if("-1".equals(line)) {
                                   check=false;
                               }
                           }
                           line = readFromConsol.readLine(); 
                           if(line.equalsIgnoreCase("2")) {
                               break;
                           }else
                           {
                             writeToStream.writeUTF(line);
                            BufferedImage img=ImageIO.read(
                                    ImageIO.createImageInputStream(client.getInputStream()));
                            File f = new File("E:/MyFile.png");
                            f.createNewFile();
                            ImageIO.write(img, "PNG", f);
                             //process.saveImage(tmp);
                            System.out.println("Saved");
                           }


                       }
                    }catch(Exception e)
                    {
                        System.out.println(e);
                     break;
                    }
                }
                try{
                    inputFromStream.close();
                    readFromConsol.close();
                    writeToStream.close();
                    this.client.close();
                }catch(Exception e){
                    System.out.println("Error : "+e.getMessage());
                }
    }

問題是所有命令都已成功提交,直到圖像接收圖像停止不動為止

嘗試沖洗發送套接字上的流出,然后關閉套接字。

問題似乎是,在清空套接字並關閉套接字之前,接收方IOImage.read()將等待流中是否有更多圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM