is Any Port Ok for an IP address - Android Network

Android examples for Network:IP Address

Description

is Any Port Ok for an IP address

Demo Code


//package com.java2s;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.SocketAddress;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class Main {

    public static boolean isAnyPortOk(String ip) {
        int portArray[] = { 80, 135, 137, 139, 8081, 3389, 3511, 3526,
                62078 };/* ww  w  . j  a va  2s  .  com*/

        Selector selector;
        try {
            selector = Selector.open();
            for (int i = 0; i < portArray.length; i++) {
                SocketChannel channel = SocketChannel.open();
                SocketAddress address = new InetSocketAddress(ip,
                        portArray[i]);
                channel.configureBlocking(false);
                channel.connect(address);
                channel.register(selector, SelectionKey.OP_CONNECT, address);
                if (selector.select(1500) != 0) {
                    selector.close();
                    return true;
                } else {
                    selector.close();
                    return false;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Related Tutorials