Вопрос

I am supposed to develop a simple SFTP.
Things were going fine until my ByteArrayOutputStream (in this case, baos) was not writing all of the arrayByte. Can some please explain to me, why the system hangs on me?

Server Side:

public static void main (String[] args)
{
        int portTexto = 5656;
        String comando;
        String regexGet = "\\s*(get)\\s[A-z0-9]*\\.[A-z0-9]*\\s*";
        String regexPut = "\\s*(put)\\s[A-z0-9]*\\.[A-z0-9]*\\s*";

        try{

            ServerSocket servSockTexto = new ServerSocket(portTexto);

            // pegar IP de servidor.
            try {
                  InetAddress addr = InetAddress.getLocalHost();            
                  System.out.println(addr.getHostAddress());
            } 
            catch (UnknownHostException e) {
            }

            System.out.println("Aguardando conexão com Servidor de Texto...");
            Socket sock = servSockTexto.accept(); 
            System.out.println("Conexão com Servidor estabelecida.");

            //BufferedOutputStream paraCliente = new BufferedOutputStream(sock.getOutputStream());

            DataInputStream inputServ = new DataInputStream(sock.getInputStream());
            DataOutputStream outputServ = new DataOutputStream(sock.getOutputStream());

            BufferedOutputStream paraCliente = new BufferedOutputStream(sock.getOutputStream());

            //1
            outputServ.writeUTF("Conexão com Servidor estabelecida.");

            do
            {

                //3
                comando = inputServ.readUTF();

                if(comando.matches(regexGet))
                {

                    String[] aux = comando.split("\\s");
                    String nomeArq = aux[1];

                    File arqGet = new File(nomeArq);

                    //4
                    outputServ.writeUTF(arqGet.getName());

                    byte[] arrayByte = new byte[(int) arqGet.length()];


                    FileInputStream fInput = new FileInputStream(arqGet);
                    BufferedInputStream bInput = new BufferedInputStream(fInput);

                    bInput.read(arrayByte, 0, arrayByte.length);

                    //5
                    paraCliente.write(arrayByte.length);

                    //6
                    paraCliente.write(arrayByte, 0, arrayByte.length);

                    paraCliente.flush();
                    bInput.close();
                    fInput.close();

                }
                else if(comando.matches(regexPut))
                {

                }
                else if (!comando.equals("sair"))
                {
                    //4
                    outputServ.writeUTF("Comando não existente.");
                }

            }
            while(!comando.equals("sair"));

            servSockTexto.close();
            sock.close();
            inputServ.close();
            outputServ.close();
            paraCliente.close();

        }
        catch (Exception ex)
        {
            System.out.println("Erro: " + ex);
        }

    }

Client Side:

    public static void main (String[] args)
    {
        String IP = "localhost";
        String comando;
        String regexGet = "\\s*(get)\\s[A-z0-9]*\\.[A-z0-9]*\\s*";
        String regexPut = "\\s*(put)\\s[A-z0-9]*\\.[A-z0-9]*\\s*";
        int portTexto = 5656;
        Scanner scan = new Scanner(System.in);


        try{

            Socket sock = new Socket (IP,portTexto); 

            //BufferedInputStream is = new BufferedInputStream(sock.getInputStream());

            DataInputStream inputCli = new DataInputStream(sock.getInputStream());
            DataOutputStream outputCli = new DataOutputStream(sock.getOutputStream());

            InputStream is = sock.getInputStream();
            //BufferedInputStream is = new BufferedInputStream(sock.getInputStream());

            //1
            System.out.println(inputCli.readUTF());


            do
            {
                System.out.println("Digite um comando:");
                comando = scan.nextLine();

                //3
                outputCli.writeUTF(comando);

                if(comando.matches(regexGet))
                {

                    //4
                    String arqTeste = "C:\\Users\\Frederico\\Desktop\\Download SFTP\\" + inputCli.readUTF();


                    int bytesRead;


        ByteArrayOutputStream baos = new      ByteArrayOutputStream();                         FileOutputStream fos = new FileOutputStream( arqTeste );
                  BufferedOutputStream bos = new BufferedOutputStream(fos);

                    //5
                    byte[] aByte = new byte[1];


                    //6
                    bytesRead = is.read(aByte, 0, aByte.length);



                     // MY SYSTEM STAYS INSIDE THIS DO AND I CANT SOLVE THIS!!!  
                    do 
                    {
                            baos.write(aByte);
                            bytesRead = is.read(aByte);

                    } 
                    while (bytesRead != -1);


                    //is.close();
                    bos.write(baos.toByteArray());
                    bos.flush();
                    bos.close();


                }
                else if(comando.matches(regexPut))
                {}
                else if (!comando.equals("sair"))
                {
                    //4
                    System.out.println(inputCli.readUTF());
                }


            }
            while(!comando.equals("sair"));

            is.close();
            sock.close();
            inputCli.close();
            outputCli.close();

            System.out.println("Conexão com servidor encerrada.");

        }
        catch (Exception ex)
        {
            System.out.println("Erro: " + ex);
        }

    }
Это было полезно?

Решение

Your do/while loop runs until end of stream but the peer is never closing the socket. The protocol seems to require keeping the socket open for further commands, so you will have to adjust this part of it to include a length-word prefix so you know how many bytes to copy.

The question isn't about ByteArrayOutputStream not writing all your bytes, it is about blocking in is.read().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top