【发布时间】:2017-06-02 09:19:51
【问题描述】:
我正在尝试在本地网络上建立一个简单的 TLS 连接。客户端仅在关闭输出流(输出)后才会收到我要发送的消息。关闭输出流也会关闭套接字。另一个问题是客户端没有正确编码消息(他得到类似 &��!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);
}
}
【问题讨论】:
-
“客户端只有在关闭输出流后才会收到我要发送的消息” - 如果不关闭输出流会怎样?
-
如果我不关闭流,什么都不会发生。
-
您刚刚编辑的客户端代码证明了我的观点。