0

我正在尝试制作一个客户端服务器应用程序,其中服务器列出可用图像的名称,客户端选择其中一个进行下载,例如这里:

服务器中的线程

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

问题是所有命令都成功提交,直到图像接收图像停止在那里不动

4

1 回答 1

0

尝试在发送套接字上刷新外流,然后关闭套接字。

问题似乎是,在套接字被刷新并关闭之前,接收IOImage.read()将等待认为流中有更多图像。

于 2014-09-06T13:54:23.197 回答