Android™
Android™
Move a Connection into Another Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.TakeConnection, which moves the active connection from one Socket into another. The receiving object first releases any connection it previously held.
Background. Unlike TakeSocket, TakeConnection does not add a member to a socket set. After success, the source socket holds no connection. This is useful for handing an accepted connection to a dedicated handler object.
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 listenSock = new CkSocket();
success = listenSock.BindAndListen(5000,25);
if (success == false) {
Log.i(TAG, listenSock.lastErrorText());
return;
}
CkSocket connectedSock = new CkSocket();
success = listenSock.AcceptNext(20000,connectedSock);
if (success == false) {
Log.i(TAG, listenSock.lastErrorText());
return;
}
// Move the active connection out of connectedSock and into another Socket object. Unlike
// TakeSocket, this does not add a member to a socket set. After success, connectedSock holds no
// connection.
CkSocket handlerSock = new CkSocket();
success = handlerSock.TakeConnection(connectedSock);
if (success == false) {
Log.i(TAG, handlerSock.lastErrorText());
return;
}
Log.i(TAG, "Connection moved to the handler socket.");
}
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."
}
}