应用程序只是在处理线程时挂起?

时间:2012-04-19 19:46:23

标签: java android ssl

设置了一个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();

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

应用程序在“RFB Socket created”之后挂起,然后在logcat中按意图记录活动。提前谢谢。

1 个答案:

答案 0 :(得分:0)

由于使用了ssl服务器套接字并且我拥有自己的java主机,因此需要手动启动握手协议。这对我有用!