Code example for ServerSocket

Methods: getLocalPort, setSoTimeout

0
  private ServerSocket socket;
  private Thread thread;
 
  public void init() { 
    try { 
      socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] {127,0,0,1}));
      socket.setSoTimeout(5000);
      port = socket.getLocalPort();
      Log.d(LOG_TAG, "port " + port + " obtained");
    } catch (UnknownHostException e) {
      Log.e(LOG_TAG, "Error initializing server", e);
    } catch (IOException e) {
      Log.e(LOG_TAG, "Error initializing server", e);
    } 
  } 
 
  public void start() { 
 
    if (socket == null) {
      throw new IllegalStateException("Cannot start proxy; it has not been initialized.");
    }