Android™
Android™
Bind and Listen for Incoming Connections
See more Socket/SSL/TLS Examples
Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.
Background. The backlog argument bounds the queue of pending connections. When
ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.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();
// Bind a TCP listener to a port and place it into listening mode. The 2nd argument is the backlog,
// the maximum number of pending connections allowed to queue.
success = socket.BindAndListen(5000,25);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
// After the listener is established, accept the next incoming connection into a new Socket.
CkSocket connectedSock = new CkSocket();
success = socket.AcceptNext(20000,connectedSock);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
Log.i(TAG, "Accepted a connection.");
}
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."
}
}