小编典典

为什么InputStream.read()在这里阻塞?

java

我应该开发一个简单的SFTP。
一切都进行得很好,直到我ByteArrayOutputStream(在本例中baos)没有编写全部为止arrayByte。可以请我解释一下,为什么系统挂在我身上吗?

服务器端:

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

    }

客户端:

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

    }

阅读 372

收藏
2020-11-30

共1个答案

小编典典

您的do/while循环一直运行到流结束,但是对等方永远不会关闭套接字。该协议似乎要求打开套接字以供其他命令使用,因此您必须调整它的这一部分以包括一个长度字前缀,以便您知道要复制多少字节。

问题不是关于ByteArrayOutputStream不写所有字节,而是关于阻塞in is.read()

2020-11-30