有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么客户端仅在关闭此TLS连接中的流后才获取消息?

我试图在本地网络上建立一个简单的TLS连接。客户端仅在关闭输出流(out)后才收到我试图发送的消息。关闭输出流也会关闭socket。另一个问题是,客户机没有正确编码消息(他得到了类似&;��!Y�S} y.?'锌:�܋�zC英镑Cw8�J��=�������R��y)

我需要使用输入和输出流来来回发送消息

我使用了来自Oracle's websiteKnockKnockProtocol

    ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
    Socket clientSocket = serverSocket.accept();

    TlsServerProtocol tlserver = new TlsServerProtocol(clientSocket.getInputStream(),
            clientSocket.getOutputStream(), new SecureRandom());


    tlserver.accept(new MyTlsServer(certificateData));

    try (

        // to send to client
        PrintWriter out = new PrintWriter(tlserver.getOutputStream(), true);
        // to receive from client
        BufferedReader in = new BufferedReader(new InputStreamReader(tlserver.getInputStream()));
        ) {


        String inputLine, outputLine;

        KnockKnockProtocol kkp = new KnockKnockProtocol();
        outputLine = kkp.processInput(null);
        out.println(outputLine);
        out.flush();
        out.close();

        while ((inputLine = in.readLine()) != null) {
            System.out.println("point 2");
            outputLine = kkp.processInput(inputLine);
            out.println(outputLine);
            if (outputLine.equals("Bye."))
                break;
        }


    }
    } catch (IOException e) {
        System.out.println(
                "Exception caught when trying to listen on port " + PORT_NUMBER + " or listening for a connection");
        System.out.println(e.getMessage());
    }


}

客户端代码:

Socket socket = new Socket(hostName, portNumber);
    TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream(),new SecureRandom());

    // client
    DefaultTlsClient client = new DefaultTlsClient() {
        // getAuthentication should be implemented
        public TlsAuthentication getAuthentication() throws IOException {

            TlsAuthentication auth = new TlsAuthentication() {
                // Capture the server certificate information!

                // Called by the protocol handler to report the server certificate Note: this method is responsible for certificate verification and validation
                public void notifyServerCertificate(org.bouncycastle.crypto.tls.Certificate serverCertificate) throws IOException {
                }

                // Return client credentials in response to server's certificate request
                public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {


                    return null;
                }
            };
            return auth;
        }
    };
    protocol.connect(client);


    try (
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        ) {
            BufferedReader stdIn =
                new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;

            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye."))
                    break;

                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                hostName);
            System.exit(1);
        }

}

共 (1) 个答案

  1. # 1 楼答案

    why does the client gets the message only after closing the stream in this tls connection?

    因为客户端必须处于一个只在流结束时终止的读循环中,这只在对等方关闭连接时发生。如果您发布了客户机代码,这会有所帮助,但这是不可避免的

    The problem is that Closing the output stream closes the socket too.

    这不是“问题”。这就是way it works - Javadoc

    Another problem is that the client isn't getting the message correctly (a problem with the encoding)

    编码有什么问题?如果你有这样的问题,发布它但是您发布的代码唯一可能发生的事情是readLine()方法中的SocketException: socket closed

    I need to use the input and output streams to send messages back and forth.

    所以不要关闭它们,若你们不想得到它,那个么在流结束之前不要阅读