Sample code for 30+ languages & platforms
Android™

Move a Connection into a Socket Set

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.

Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.

Chilkat Android™ Downloads

Android™
// 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 connection out of connectedSock and into a newly created member of this object's socket
    //  set.  After one or more such calls, socketSet acts as a set of connected sockets (useful with the
    //  SelectForReading / SelectForWriting methods).  After success, connectedSock holds no connection.
    CkSocket socketSet = new CkSocket();
    success = socketSet.TakeSocket(connectedSock);
    if (success == false) {
        Log.i(TAG, socketSet.lastErrorText());
        return;
        }

    Log.i(TAG, "Connection added to the socket set.");

  }

  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."
  }
}