gpt4 book ai didi

java - 为什么客户端只有在关闭此 TLS 连接中的流后才会收到消息?

转载 作者:太空宇宙 更新时间:2023-11-03 15:15:53 26 4
gpt4 key购买 nike

我正在尝试在本地网络上建立一个简单的 TLS 连接。只有在关闭输出流 (out) 之后,客户端才会收到我尝试发送的消息。关闭输出流也会关闭套接字。另一个问题是客户端没有得到正确编码的消息(他得到类似 &��!y��S}y.?'zn:����zC£Cw8��J��=������ �R��y)

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

我使用了 Oracle's website 中的 KnockKnockProtocol

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

}

最佳答案

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.

所以不要关闭它们,如果您不打算读取它,请不要阅读直到流结束。

关于java - 为什么客户端只有在关闭此 TLS 连接中的流后才会收到消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com