简体   繁体   English

通过Java中的TCP套接字发送图像

[英]Sending image Through TCP Socket in Java

I'm trying to make a Client Server Application which the server list name of available images and the client select one of them to download Such as here : 我正在尝试制作一个客户端服务器应用程序,其中服务器列表中的可用图像名称和客户端选择其中之一进行下载,例如:

Thread in Server 服务器中的线程

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

     }  

Client : 客户:

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

The problem is that all commands are successfully submitted till image receiving image stop there doesn't move 问题是所有命令都已成功提交,直到图像接收图像停止不动为止

Try doing a flush of the outstream on the sending socket, then close the socket. 尝试冲洗发送套接字上的流出,然后关闭套接字。

The problem appears to be that until the socket is flushed and closed the receiving IOImage.read() will wait thinking there are more images in the stream. 问题似乎是,在清空套接字并关闭套接字之前,接收方IOImage.read()将等待流中是否有更多图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM