Have setup an ssl connection app, have already setup connection between client and server using single thread. Since my app deals with remote desktop access, i am trying to implement threading.
Don’t know why the application just stops after creating socket, it neither fails nor performs SSLhandshake. Same code when executed under single thread works smoothly, but under multi-thread its not. Have tried logcat, it couldnt help me out. Out of 10-15 tries, application works fine 1 time, but goes to not responsive mode.
Any idea where i am hitting wrong ?Here is the code. Any answer please do share.
Main activity:
Thread t = new Thread() {
public void run() {
try{
Log.d(TAG, "Opening RFB socket");
rfb = new rfbProtocol(ip, port, RFB_ClientActivity.getContext());
txt_notify.append("Connection done");
Log.d(TAG, "RFB socket openned");
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
progress.setMessage("Connection established..\nPlease wait");
}
});
processProtocol(progress);
}catch(Exception e){
if(maintainConnection){
if(progress.isShowing())
progress.dismiss();
txt_notify.append( "Connection failure:\n" + e.getMessage() );
Log.d(TAG,"RFB socket failure");
e.printStackTrace();
}
}
}
};
t.start();
rfbProtocol:
rfbProtocol(String h, int p, Context c) throws Exception {
// TODO Auto-generated constructor stub
host = h;
port = p;
cont = c;
try {
// Setup the SSL context to use the truststore and keystore
Log.d(TAG, "Initializing SSL connection");
SSLContext ssl_context = createSSLContext(cont);
SSLSocketFactory socketFactory = (SSLSocketFactory) ssl_context.getSocketFactory();
Log.d(TAG,"Creating RFB socket");
socket = (SSLSocket) socketFactory.createSocket(host, port);
Log.d(TAG,"RFB Socket created");
dataOut = socket.getOutputStream();
dataIn = new DataInputStream(new BufferedInputStream(socket.getInputStream(), 16384));
close = false;
Log.d(TAG,"SSL connection done");
} catch (Exception e) {
// TODO Auto-generated catch block
throw(e);
}
}
private SSLContext createSSLContext(final Context cont) throws Exception{
SSLContext ssl_cont = null;
try {
// Setup truststore
Log.d(TAG, "TrustStore - Initializing");
KeyStore trustStore = KeyStore.getInstance("BKS");
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
InputStream trustStoreStream = cont.getResources().openRawResource(R.raw.clienttruststore);
trustStore.load(trustStoreStream, "client".toCharArray());
trustManagerFactory.init(trustStore);
Log.d(TAG, "TrustStore - Initialized");
// Setup keystore
Log.d(TAG, "KeyStore - Initializing");
KeyStore keyStore = KeyStore.getInstance("BKS");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
InputStream keyStoreStream = cont.getResources().openRawResource(R.raw.client);
keyStore.load(keyStoreStream, "client".toCharArray());
keyManagerFactory.init(keyStore, "client".toCharArray());
Log.d(TAG, "KeyStore - Initialized");
ssl_cont = SSLContext.getInstance("TLS");
ssl_cont.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
} catch (Exception e) {
// TODO Auto-generated catch block
throw(e);
}
return ssl_cont;
}
Application just hangs after “RFB Socket created”, followed by activity logged by intent in the logcat. Thanks in advance.
Since ssl server socket is used and i have my own java host, need to initiate handshake protocol manually. This worked for me !