sslsocket只从输入流中读取一次,为什么?

时间:2016-09-30 02:24:19

标签: java android sockets inputstream sslsocketfactory

与服务器的部分连接已完成,但无法始终从输入流中读取数据,只能读取一次。

    public void startSSLClient() throws IOException {
    inf = new Inflater();
    mSocketBufferReader = new BufferedReader(new InputStreamReader(mSslSocket.getInputStream()));
    new Thread() {
        boolean online =false;
        String s;
        @Override
        public void run() {
            try {
                while ( true ) {
                    String str = mSocketBufferReader.readLine();
                    if (!TextUtils.isEmpty(str)) {
                        Logger.e("PacketReader","process packet : "+str);
                    }
                }
            }
            catch (Exception e) {
                System.out.println("Error: " + e);
            }
        }
    }.start();
}

如果我尝试这个,它的工作总是从输入流读取。

    public void startSSLClient() throws IOException {
    inf = new Inflater();

    new Thread() {
        boolean online =false;
        String s;
        @Override
        public void run() {
            try {
                while ( true ) {
                    BufferedReader mSocketBufferReader = new BufferedReader(new InputStreamReader(mSslSocket.getInputStream()));
                    String str = mSocketBufferReader.readLine();
                    if (!TextUtils.isEmpty(str)) {
                        Logger.e("PacketReader","process packet : "+str);
                    }
                }
            }
            catch (Exception e) {
                System.out.println("Error: " + e);
            }
        }
    }.start();
}

我不知道原因。我只想使用全局字段。我不想总是使用新的本地字段。

1 个答案:

答案 0 :(得分:0)

 BufferedReader mSocketBufferReader = new BufferedReader(new InputStreamReader(mSslSocket.getInputStream()));

把它拿出来。您只能为连接执行一次该语句。