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 ? Have attached the code. Any answer
please do share.
RFB_ClientActivity is the main activity, in which i have started new
activity RFB_Activity. RFB_Activity handles job of creating ssl socket
connection via async thread but applications just hang's after creating
sockets, it is not able to create the output and input stream & think its
taking long time to preform handshake.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
04-20 23:57:01.769 I/InputDispatcher(159): Delivering key to current input
target: action: 0, channel '408cdbd0
com.rfb.client/com.rfb.client.RFB_ClientActivity (server)'
04-20 23:57:01.859 I/InputDispatcher(159): Delivering key to current input
target: action: 1, channel '408cdbd0
com.rfb.client/com.rfb.client.RFB_ClientActivity (server)'
04-20 23:57:02.279 I/InputDispatcher(159): Delivering touch to current input
target: action: 0, channel '408cdbd0
com.rfb.client/com.rfb.client.RFB_ClientActivity (server)'
04-20 23:57:02.339 I/InputDispatcher(159): Delivering touch to current input
target: action: 1, channel '408cdbd0
com.rfb.client/com.rfb.client.RFB_ClientActivity (server)'
04-20 23:57:02.339 D/RFB_MAIN(18199): RFB Activity started
04-20 23:57:02.339 I/ActivityManager(159): Starting: Intent {
cmp=com.rfb.client/.RFB_Activity (has extras) } from pid 18199
04-20 23:57:02.359 W/ActivityManager(159): Trying to launch
com.rfb.client/.RFB_Activity
04-20 23:57:02.429 D/RFB_Activity(18199): Opening RFB socket
04-20 23:57:02.719 D/RFB_Async(18199): Initiating
04-20 23:57:02.719 D/RFB_PROTOCOL(18199): Initializing SSL connection
04-20 23:57:02.719 D/RFB_PROTOCOL(18199): TrustStore - Initializing
04-20 23:57:02.729 I/ActivityManager(159): Displayed
com.rfb.client/.RFB_Activity: +369ms
04-20 23:57:04.309 D/RFB_PROTOCOL(18199): TrustStore - Initialized
04-20 23:57:04.309 D/RFB_PROTOCOL(18199): KeyStore - Initializing
04-20 23:57:04.619 D/RFB_PROTOCOL(18199): KeyStore - Initialized
04-20 23:57:05.169 D/RFB_PROTOCOL(18199): Creating RFB socket
04-20 23:57:05.909 D/RFB_PROTOCOL(18199): RFB Socket created
04-20 23:57:20.749 I/ActivityManager(159): Starting: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
flg=0x10100000 cmp=com.rfb.client/.RFB_ClientActivity bnds=[182,205][238,271] }
from pid 159
04-20 23:57:43.729 I/ActivityManager(159): Starting: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
flg=0x10100000 cmp=com.rfb.client/.RFB_ClientActivity bnds=[182,205][238,271] }
from pid 159
04-20 23:58:35.289 I/ActivityManager(159): Starting: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
flg=0x10100000 cmp=com.rfb.client/.RFB_ClientActivity bnds=[182,205][238,271] }
from pid 159
04-20 23:59:18.349 W/System.err(18199): at
com.rfb.client.rfbProtocol.<init>(rfbProtocol.java:43)
04-20 23:59:18.349 W/System.err(18199): at
com.rfb.client.getting_rfb.doInBackground(getting_rfb.java:23)
04-20 23:59:18.349 W/System.err(18199): at
com.rfb.client.getting_rfb.doInBackground(getting_rfb.java:1)
04-20 23:59:18.359 D/RFB_Activity(18199): RFB socket openned
04-20 23:59:18.359 D/RFB_Activity(18199): RFB socket failure
04-20 23:59:18.359 W/System.err(18199): at
com.rfb.client.RFB_Activity.processProtocol(RFB_Activity.java:90)
04-20 23:59:18.359 W/System.err(18199): at
com.rfb.client.RFB_Activity$2.run(RFB_Activity.java:67)
package com.rfb.client;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class getting_rfb extends AsyncTask<Context, Void, rfbProtocol> {
private String host, TAG = "RFB_Async";
private int port;
public getting_rfb(String h,int p){
host = h;
port = p;
}
@Override
protected rfbProtocol doInBackground(Context... context) {
// TODO Auto-generated method stub
rfbProtocol rfb = null;
try {
Log.d(TAG, "Initiating");
rfb = new rfbProtocol(host, port, context[0]);
Log.d(TAG, "Done");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d(TAG,"Failure");
e.printStackTrace();
}
return rfb;
}
}
package com.rfb.client;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class RFB_Activity extends Activity {
public Handler handler = new Handler();
public Boolean maintainConnection = true;
public String TAG = "RFB_Activity";
public TextView txt_notify;
public rfbProtocol rfb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
Intent intent = getIntent();
txt_notify = (TextView) findViewById(R.id.txt_msg);
String host = intent.getExtras().getString("host");
int port = intent.getExtras().getInt("port");
intializeRFB(host, port);
// TODO Auto-generated method stub
}
void intializeRFB(final String ip,final int port){
final ProgressDialog progress = ProgressDialog.show(this, "Connecting", "Establishing RFB connection..\nPlease wait...", true, true, new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
closeConnection();
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txt_notify.append("RFB connection aborted !");
}
});
}
});
final getting_rfb rfb_async = new getting_rfb(ip, port);
rfb_async.execute(RFB_ClientActivity.getContext());
Thread t = new Thread() {
public void run() {
try{
Log.d(TAG, "Opening RFB socket");
// rfb = new rfbProtocol(ip, port, RFB_ClientActivity.getContext());
rfb = rfb_async.get();
Log.d(TAG, "RFB socket openned");
if(rfb != null){
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
progress.setMessage("Connection established..\nPlease wait");
txt_notify.append("Connection done");
}
});
}
else
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
progress.setMessage("Connection failed..");
txt_notify.append("Server is dead");
}
});
}catch(Exception e){
if(maintainConnection){
if(progress.isShowing())
progress.dismiss();
Log.d(TAG,"RFB socket failure");
e.printStackTrace();
}
}
}
};
t.start();
}
void closeConnection(){
maintainConnection = false;
}
void processProtocol( ProgressDialog pd )throws Exception{
if(pd.isShowing())
pd.dismiss();
try{
while(maintainConnection){
final String msg = rfb.readMsg();
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
txt_notify.append("Received : " + msg + "\n");
}
});
}
}catch(Exception e){
throw(e);
}
}
}
package com.rfb.client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class RFB_ClientActivity extends Activity {
private EditText txt_ip,txt_port;
private Button conn;
private static Context context;
private static final String TAG = "RFB_MAIN";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
txt_ip = (EditText) findViewById(R.id.ipadd);
txt_port = (EditText) findViewById(R.id.port);
conn = (Button) findViewById(R.id.btconn);
conn.setOnClickListener(onConnClick);
}
OnClickListener onConnClick = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ip;
int port;
ip = txt_ip.getText().toString().trim();
port = Integer.parseInt(txt_port.getText().toString().trim());
Intent intent = new Intent(context, RFB_Activity.class);
intent.putExtra("host", ip);
intent.putExtra("port", port);
Log.d(TAG, "RFB Activity started");
startActivity(intent);
}
};
static Context getContext(){
return context;
}
}
package com.rfb.client;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import android.content.Context;
import android.util.Log;
public class rfbProtocol{
Boolean close = true;
String host;
int port;
SSLSocket socket;
OutputStream dataOut;
DataInputStream dataIn;
Context cont;
String TAG = "RFB_PROTOCOL";
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;
}
synchronized void close(){
try {
socket.close();
close = true;
Log.d(TAG, "RFB socket closed");
}catch(Exception e){
Log.d(TAG, "RFB socket closing failure");
e.printStackTrace();
}
}
synchronized boolean isClosed(){
return close;
}
String readMsg()throws Exception{
return dataIn.readUTF();
}
}