gpt4 book ai didi

java - JSSE 中的连接重置

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:23 25 4
gpt4 key购买 nike

我正在尝试做基于客户端服务器的 ssl,

这是我的服务器代码:

 public static void main(String arg[]){

try {
KeyStore keyStore = KeyStore.getInstance("jks");

InputStream inputStream;
try {
inputStream = new FileInputStream(ITestEncryptionConstants.SSLKEYSTORE);
keyStore.load(inputStream,
ITestEncryptionConstants.SSLPASSWORD.toCharArray());
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance("PKIX", "SunJSSE");
trustManagerFactory.init(keyStore);

X509TrustManager x509TrustManager = null;
for (TrustManager trustManager : trustManagerFactory.getTrustManagers())
{
if (trustManager instanceof X509TrustManager) {
x509TrustManager = (X509TrustManager) trustManager;
break;
}
}

KeyManagerFactory keyManagerFactory =
KeyManagerFactory.getInstance("SunX509", "SunJSSE");
keyManagerFactory.init(keyStore,
ITestEncryptionConstants.SSLPASSWORD.toCharArray());

X509KeyManager x509KeyManager = null;
for (KeyManager keyManager : keyManagerFactory.getKeyManagers()) {
if (keyManager instanceof X509KeyManager) {
x509KeyManager = (X509KeyManager) keyManager;
break;
}
}

SSLContext sslContext = SSLContext.getInstance("TLS");

sslContext.init(new KeyManager[]{x509KeyManager},
new TrustManager[]{x509TrustManager}, null);

SSLServerSocketFactory factory =
sslContext.getServerSocketFactory();//(SSLServerSocketFactory )
SSLServerSocketFactory .getDefault();

SSLServerSocket sslSock =
(SSLServerSocket)factory.createServerSocket(8096);
SSLSocket c = (SSLSocket)sslSock.accept();
PrintWriter out =new PrintWriter(c.getOutputStream(),true);
InputStream in = c.getInputStream();
in.close();
out.close();


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



}

这是我的客户端代码:

 public static void main(String[] args) {

try{
// // register a https protocol handler - this may be required for previous JDK versions
//
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");



System.out.println("Ok passed.");
//connect to google
SSLSocketFactory factory = (SSLSocketFactory)
SSLSocketFactory.getDefault();
SSLSocket sslSock = (SSLSocket) factory.createSocket("localhost",8096);

//send HTTP get request
PrintWriter out = new PrintWriter(sslSock.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(sslSock.getInputStream()));

out.println("--------------------------> It works!!!");
System.out.println("Written to server.");

// read response
StringBuffer buf = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
System.out.println("Received : " + line);
buf.append(line);
}
System.out.println("Read from server: " + buf.toString());

in.close();
out.close();
// Close connection.
sslSock.close();

}catch(Exception ex){
ex.printStackTrace();
}
}

但我在客户端收到以下错误:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset

有人可以帮助解决我的代码中出现的问题吗?

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset
at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at ripl.jing.security.encryptor.tlsservice.TestJingClient.main(TestJingClient.java:85)
Caused by: javax.net.ssl.SSLException: java.net.SocketException: Connection reset

最佳答案

(抱歉,我只是想评论 EJP 现已删除的答案。)

服务器代码说:

SSLSocket c = (SSLSocket)sslSock.accept();
PrintWriter out =new PrintWriter(c.getOutputStream(),true);
InputStream in = c.getInputStream();
in.close();
out.close();

也就是说,I/O 流(以及套接字)在接受套接字后立即关闭。因此,客户端上出现一条错误消息,表明连接已被远程方关闭,这真的不足为奇。

这或多或少与 this question 中的问题相同: 只获取 I/O 流,不尝试从中读取/写入,也不尝试任何其他隐式触发握手的操作(从流中读取或尝试获取 SSLSession)或显式(调用 startHandshake)意味着握手不会在服务器上启动。因此,客户端抛出此异常,因为连接在其已开始的握手过程中中断。

关于java - JSSE 中的连接重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516609/

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