Java Utililty Methods InetAddress

List of utility methods to do InetAddress

Description

The list of methods to do InetAddress are organized into topic(s).

Method

URLcreateAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot)
create Absolute URL
try {
    if (address instanceof Inet6Address) {
        return createAbsoluteURL(new URL("http://[" + address.getHostAddress() + "]:" + localStreamPort),
                relativeOrNot);
    } else if (address instanceof Inet4Address) {
        return createAbsoluteURL(new URL("http://" + address.getHostAddress() + ":" + localStreamPort),
                relativeOrNot);
    } else {
...
DatagramSocketcreateDatagramSocket(InetAddress addr, int port)
Creates a DatagramSocket bound to addr.
DatagramSocket sock = null;
if (addr == null) {
    if (port == 0) {
        return new DatagramSocket();
    } else {
        while (port < MAX_PORT) {
            try {
                return new DatagramSocket(port);
...
InetSocketAddresscreateInetSocketAddress(final InetAddress address)
create Inet Socket Address
if (address.isLoopbackAddress()) {
    return new InetSocketAddress(address, LOCAL_CLIENT_PORT);
return new InetSocketAddress(address, CLIENT_PORT);
InetSocketAddresscreateResolved(InetAddress address, int port)
Creates and returns a resolved InetSocketAddress .
return new InetSocketAddress(address, port);
DatagramSocketcreateSocket(InetAddress address, int port, boolean fixedPort)
Opens a socket on a specific network interface, either on a fixed or a random port.
DatagramSocket result = null;
if (fixedPort) {
    try {
        result = new DatagramSocket(new InetSocketAddress(address, port));
        result.setSoTimeout(50);
    } catch (Exception e) {
        return null;
} else {
    Random rdm = new Random();
    port = port + rdm.nextInt(1024);
    while (result == null && port < 65530) {
        try {
            result = new DatagramSocket(new InetSocketAddress(address, port));
            result.setSoTimeout(50);
        } catch (Exception e) {
            result = null;
            port++;
return result;
booleandoMatch(InetAddress inetAddress, String toMatchParam)
Checks a string macthes with a IP address.
String hostAddres = inetAddress.getHostAddress();
if (toMatchParam.contains("-")) {
    if (hostAddres.contains(":")) {
        return false;
    String[] octects = toMatchParam.split("\\.");
    String[] octectsIA = hostAddres.split("\\.");
    String first3 = octects[0] + "." + octects[1] + "." + octects[2];
...
intfindFreePort(InetAddress address, int start, int end)
Find free port in range for the specified IP address
ServerSocket socket = null;
try {
    for (int port = start; port <= end; ++port) {
        try {
            socket = new ServerSocket(port, 0, address);
            socket.setReuseAddress(true);
            return socket.getLocalPort();
        } catch (IOException ignore) {
...
intfindUnusedPort(InetAddress address, int from, int to)
find Unused Port
for (int i = 0; i < 12; i++) {
    ServerSocket ss = null;
    int port = getRandomPort(from, to);
    try {
        ss = new ServerSocket();
        SocketAddress sa = new InetSocketAddress(address, port);
        ss.bind(sa);
        return ss.getLocalPort();
...
intfindUnusedPort(InetAddress address, int low, int high)
Finds an unused local port between the given from and to values.
if (high < low)
    return -1;
for (int i = 0; i < 10; i++) {
    int port = getRandomPort(low, high);
    if (!isPortInUse(address, port))
        return port;
return -1;
...
ScriptableformatAddress(InetAddress address, int port, Context cx, Scriptable scope)
format Address
Scriptable addr = cx.newObject(scope);
addr.put("port", addr, port);
addr.put("address", addr, address.getHostAddress());
if (address instanceof Inet6Address) {
    addr.put("family", addr, "IPv6");
} else {
    addr.put("family", addr, "IPv4");
return addr;