已设置 ssl 连接应用程序,已使用单线程在客户端和服务器之间设置连接。由于我的应用程序处理远程桌面访问,我正在尝试实现线程。
不知道为什么应用程序在创建套接字后就停止了,它既没有失败也没有执行 SSLhandshake。在单线程下执行相同的代码可以顺利运行,但在多线程下则不行。已经尝试过logcat,它无法帮助我。在 10-15 次尝试中,应用程序运行良好 1 次,但进入无响应模式。
知道我在哪里打错了吗?这是代码。任何答案请分享。
主要活动:
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();
rfb协议:
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;
}
应用程序只是在“创建 RFB 套接字”之后挂起,然后是 logcat 中意图记录的活动。提前致谢。