简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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