Android Open Source - android_asyncsocket Tcp Server






From Project

Back to project page android_asyncsocket.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android_asyncsocket listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.smorra.asyncsocket;
//  w  w  w .  ja  v  a2s  .  c  om
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import android.os.Handler;
import android.os.Looper;

public class TcpServer extends Thread {

  private Pipe p;
  private boolean closed;
  private int port;
  private TcpServerCallback tsc;

  public TcpServer(int port, TcpServerCallback tsc) throws IOException {
    this.port = port;
    this.tsc = tsc;
    p = Pipe.open();
    p.sink().configureBlocking(true);
    p.source().configureBlocking(false);
    start();
    System.out.println("TcpServer constructor");
  }

  public void close() throws IOException {
    if (closed)
      throw new IOException("already closed");
    closed = true;
    p.sink().write(ByteBuffer.wrap(new byte[] { 0 }));
  }

  public void run() {
    System.out.println("TcpServer running");
    try {
      ServerSocketChannel ssc = ServerSocketChannel.open();
      ssc.socket().bind(new InetSocketAddress(port));
      ssc.configureBlocking(false);
      while (!closed) {
        System.out.println("TcpServer selecting");
        Selector selector = Selector.open();
        p.source().register(selector, SelectionKey.OP_READ);
        ssc.register(selector, SelectionKey.OP_ACCEPT);
        selector.select();
        for (SelectionKey k : selector.selectedKeys()) {
          System.out.println("Channel is " + k.channel());
          if (k.isAcceptable() && k.channel() == ssc) {
            System.out.println("Is acceptable");
            final SocketChannel sc = ssc.accept();
            sc.configureBlocking(false);
            new Handler(Looper.getMainLooper()).post(new Runnable() {

              @Override
              public void run() {
                try {
                  if (!closed) {
                    TcpServerClient tsc = new TcpServerClient(sc);
                    TcpServer.this.tsc.onNewClient(TcpServer.this, tsc);
                  } else {
                    sc.close();
                  }
                } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
              }
            });

          }
        }
      }
      ssc.close();

    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}




Java Source Code List

com.smorra.asyncsocket.DnsResolverCallback.java
com.smorra.asyncsocket.DnsResolver.java
com.smorra.asyncsocket.SSLClientCallback.java
com.smorra.asyncsocket.SSLClient.java
com.smorra.asyncsocket.TcpClientCallback.java
com.smorra.asyncsocket.TcpClient.java
com.smorra.asyncsocket.TcpServerCallback.java
com.smorra.asyncsocket.TcpServerClientCallback.java
com.smorra.asyncsocket.TcpServerClient.java
com.smorra.asyncsocket.TcpServer.java