有人可以告诉我我在这段代码中做错了什么吗?服务器端似乎正在工作。一旦我为服务器端运行代码,服务器就会等待客户端请求连接。然后,当我运行客户端代码时,它会在客户端引发以下异常:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at LoginDialog.doConnectToServer(LoginDialog.java:208)
at LoginDialog.<init>(LoginDialog.java:67)
at ClientSideMain.main(ClientSideMain.java:5)
Press any key to continue . . .
以及服务器端的以下异常:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: no cipher suites in common
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 Talker.recieve(Talker.java:78)
at CTC.run(CTC.java:49)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ServerHandshaker.chooseCipherSuite(Unknown Source)
at sun.security.ssl.ServerHandshaker.clientHello(Unknown Source)
at sun.security.ssl.ServerHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at Talker.send(Talker.java:58)
at CTC.send(CTC.java:344)
at CTC.<init>(CTC.java:31)
at ServerSide.<init>(ServerSide.java:73)
at ServerSideMain.main(ServerSideMain.java:5)
服务器端代码
public class ServerSide
{
ServerSocket serverSocket;
Socket regSocket;
Hashtable<String,User> userList;
CTC tempCTC;
File f;
DataInputStream in;
BufferedReader br;
SSLContext sslContext;
KeyManagerFactory keyManagerFactory;
KeyStore keyStore;
char[] keyStorePassphrase;
SSLServerSocketFactory sslServerSocketFactory;
SSLServerSocket sslServerSocket;
SSLSocket sslNormalSocket;
ServerSide()
{
f = new File("userlist.txt");
userList = new Hashtable<String, User>();
loadUsers(f);
try
{
sslContext = SSLContext.getInstance("SSL");
keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance("JKS");
keyStorePassphrase = "passphrase".toCharArray();
keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);
keyManagerFactory.init(keyStore, keyStorePassphrase);
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
sslServerSocketFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
sslServerSocket = (SSLServerSocket)sslServerSocketFactory.createServerSocket(12345);
try{Thread.sleep(1000);}catch(Exception e){}
while(true)
{
sslNormalSocket = (SSLSocket)sslServerSocket.accept();
tempCTC = new CTC(sslNormalSocket, userList, f);
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(NoSuchAlgorithmException nsae)
{
nsae.printStackTrace();
}
catch(KeyStoreException kse)
{
kse.printStackTrace();
}
catch(CertificateException ce)
{
ce.printStackTrace();
}
catch(UnrecoverableKeyException uke)
{
uke.printStackTrace();
}
catch(KeyManagementException kme)
{
kme.printStackTrace();
}
}
客户端代码(发生异常的地方)
private void doConnectToServer()
{
SSLSocketFactory sslSocketFactory;
KeyManagerFactory keyManagerFactory;
SSLContext sslContext;
KeyStore keyStore;
char[] keyStorePassphrase;
SSLSocket sslSocket;
try
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("javax.net.ssl.trustStore", "samplecacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
sslContext = SSLContext.getInstance("SSL");
keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance("JKS");
keyStorePassphrase = "passphrase".toCharArray();
keyStore.load(new FileInputStream("testkeys"), keyStorePassphrase);
keyManagerFactory.init(keyStore, keyStorePassphrase);
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
sslSocketFactory = sslContext.getSocketFactory();
sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
sslSocket = (SSLSocket)sslSocketFactory.createSocket("127.0.0.1", 12345);
sslSocket.startHandshake(); //line that throws the exception
cts = new CTS(sslSocket, this);
}
catch(Exception e)
{
e.printStackTrace();
}
}