为什么InputStream.read()阻塞在这里? [英] Why is InputStream.read() blocking here?

查看:215
本文介绍了为什么InputStream.read()阻塞在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该开发一个简单的SFTP。

在我的 ByteArrayOutputStream 之前一切正常(在这种情况下, baos )没有编写所有的 arrayByte 。有些人可以向我解释为什么系统会挂在我身上?

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?

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

    }


推荐答案

您的执行/ while 循环运行直到流结束但对等方永远不会关闭套接字。该协议似乎需要保持套接字打开以获取更多命令,因此您必须调整它的这一部分以包含长度字前缀,以便您知道要复制多少字节。

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.

问题不在于 ByteArrayOutputStream 没有编写所有字节,而是在中阻塞is.read()

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

这篇关于为什么InputStream.read()阻塞在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆