我想为多线程服务器创建一个ssl套接字,但是当连接第二个客户端时,我会遇到一些例外。
这是我的服务器代码:
public class Master implements Runnable{
public static SSLSocket sslSocket = null;
public static SSLServerSocket sslServerSocket =null;
public static SSLServerSocketFactory sslServerSocketfactory ;
Master(SSLSocket s) {
this.sslSocket = s;
}
public static void main(String args[]) throws Exception {
System.out.println("Listening");
sslServerSocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
sslServerSocket = (SSLServerSocket) sslServerSocketfactory.createServerSocket(7777);
while (true)
{
sslSocket = (SSLSocket) sslServerSocket.accept();
sslSocket.setEnabledCipherSuites(sslServerSocketfactory.getSupportedCipherSuites());
System.setProperty("javax.net.ssl.keyStore", "Master_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "passwd1");
System.out.println("Connected");
new Thread(new Master(sslSocket)).start();
}
}
public void run() {
synchronized(this){
do{
try{
//here is the error
ObjectOutputStream objout = new ObjectOutputStream(sslSocket.getOutputStream());
ObjectInputStream objin = new ObjectInputStream(sslSocket.getInputStream());
/* code */
} catch (Exception ex) {
Logger.getLogger(Master.class.getName()).log(Level.SEVERE, null, ex);}
}while(true);
}
}
}
我的客户端代码如下
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 7777);
sslsocket.setEnabledCipherSuites(sslsocketfactory.getSupportedCipherSuites());
System.setProperty("javax.net.ssl.keyStore", "Client_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "passwd2");
try {
do {
ObjectOutputStream objout = new ObjectOutputStream(sslsocket.getOutputStream());
ObjectInputStream objin = new ObjectInputStream(sslsocket.getInputStream());
/* code*/
} while (true);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
我得到的例外是:
SEVERE: null
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1541)
at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1553)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:71)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
at master.Master.run(Master.java:233)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.net.ssl.SSLHandshakeException: no cipher suites in common
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:292)
at sun.security.ssl.ServerHandshaker.chooseCipherSuite(ServerHandshaker.java:1035)
at sun.security.ssl.ServerHandshaker.clientHello(ServerHandshaker.java:738)
at sun.security.ssl.ServerHandshaker.processMessage(ServerHandshaker.java:221)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
... 5 more
有人可以给我一个解决方案吗?
答案 0 :(得分:2)
public static <T> T getParamValue(Param param) {
}
问题出在这里。绝对没有理由建立这个变量public static SSLSocket sslSocket = null;
。不要使用static
,除非您确切知道为什么这样做。