JORd JORd - 1 month ago 14
Java Question

ssl socket multithreaded server exception

I want to create an ssl socket for a multithreaded server, but when a second client is connected I get some exceptions.

Here is my server code:

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

}


And my client code is the following

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


The exceptions I get are:

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


can someone give me a solution?

EJP EJP
Answer
public static SSLSocket sslSocket = null;

The problem is here. There is absolutely no reason for making this variable static. Don't use static unless you know exactly why you are doing so.

Comments