Code example for SocketChannel

Methods: configureBlocking, connect, register

0
	public Thread connect() throws IOException {
		this.running = true;
		this.readyState = WEBSOCKET_STATE_CONNECTING;
		// open socket 
		socketChannel = SocketChannel.open();
		socketChannel.configureBlocking(false);
		// set address 
		socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
		Log.i("MonkeyPenis", "Port is " + port);
		// start a thread to make connection 
 
		// More info: 
		// http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82 
		// http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect 
		System.setProperty("java.net.preferIPv4Stack", "true");
		System.setProperty("java.net.preferIPv6Addresses", "false");
 
		selector = Selector.open();
		socketChannel.register(selector, SelectionKey.OP_CONNECT);
		Log.v("websocket", "Starting a new thread to manage data reading/writing");
 
		Thread th = new Thread(this);
		th.start();
		// return thread object for explicit closing, if needed