SSLSocket getOutputStream()在握手后阻塞

时间:2017-05-13 21:36:00

标签: java android security ssl encryption

我在客户端打开SSLSocket以连接到服务器。 但是试图让socket.getOutputStream()阻止代码,而不输出任何错误。 我正在冲洗这两个流,但它仍然不起作用。

服务器:

        SSLSocket socket = (SSLSocket) serverSocket.accept();
        socket.setUseClientMode(false);
        try{
        socket.setSoTimeout(0);
        socket.setKeepAlive(true);
        socket.addHandshakeCompletedListener(handshakeCompletedEvent -> {
            try{
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                writer.println("Setup"); writer.flush();

                while(running){
                    if(!socket.isConnected() || socket.isClosed()){
                        disconnect();
                        return;
                    }
                    for(Iterator<String> pendingIterator = pendingMessages.iterator(); pendingIterator.hasNext();){
                        String message = pendingIterator.next();
                        pendingIterator.remove();
                        writer.println(message); writer.flush();
                    }

                    //Auto decrypt when message arrives, but no thread blocking
                    if(reader.ready()){
                        String line = reader.readLine();
                        System.out.println(line);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        });
        socket.startHandshake();
    }catch(Exception e){
        e.printStackTrace();
        disconnect();
    }

客户端:

            socket.addHandshakeCompletedListener(new HandshakeCompletedListener(){
            @Override
            public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent){
                try{
                    Log.i("Update", "Did handshake");

                    writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                    writer.flush();
                    Log.i("Update", "Writer");
                    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    Log.i("Update", "Reader");

                    while(isRunning){
                        try{
                            writer.println("Test");
                            writer.flush();
                            Log.i("DATA", "DATA SENT");
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }

                    writer.close();
                    reader.close();
                    socket.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
        socket.startHandshake();

1 个答案:

答案 0 :(得分:2)

您正在滥用握手已完成的侦听器。其目的是让他检查他的同行证书,协商密码套件等。不进行交易。将其全部删除并将其内联。您甚至不需要致电startHandshake()。它将在第一个I / O上自动发生。