Android™
Android™
Upgrade a Plain TCP Connection to TLS
See more Socket/SSL/TLS Examples
Demonstrates Socket.ConvertToSsl, which upgrades an already-connected plain TCP socket to TLS by performing a client-side TLS handshake over the existing connection.
Background. This supports STARTTLS-style upgrades, where a protocol begins in plaintext and switches to TLS at a defined point. Perform the protocol-specific negotiation first, then call ConvertToSsl.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkSocket socket = new CkSocket();
// Connect as plain TCP (no TLS yet).
boolean bTls = false;
int maxWaitMs = 5000;
success = socket.Connect("example.com",5000,bTls,maxWaitMs);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
// At the point where the application protocol permits a STARTTLS-style upgrade, perform any required
// negotiation, then upgrade the existing connection to TLS with a client-side handshake.
success = socket.ConvertToSsl();
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
Log.i(TAG, "Connection upgraded to TLS.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}