Creates an InetSocketAddress given a host and optional port in a single String This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732 - Java Network

Java examples for Network:IP Address

Description

Creates an InetSocketAddress given a host and optional port in a single String This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732

Demo Code

/*//from   ww w  .j  a  va2 s  .  c  o  m
 * Copyright 2014, The Sporting Exchange Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import java.net.InetSocketAddress;

public class Main {
    public static void main(String[] argv) throws Exception {
        String address = "java2s.com";
        System.out.println(createInetSocketAddress(address));
    }

    /**
     * <p>Creates an InetSocketAddress given a host and optional port in a single String</p>
     * <p/>
     * <p>This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732</p>
     *
     * @param address the address in one of the following formats (the braces '[]'and colon ':' are literal here):
     *                host<br>
     *                [host]<br>
     *                [host]:port<br>
     *                [host]port<br>
     *                ip4host:port<br>
     *                <p>Assumes port 0 if non is specified.</p>
     *                <p/>
     *                <p> As per java.net.InetSocketAddress
     *                A port number of zero will let the system pick up an ephemeral port in a bind operation.</p>
     */
    public static InetSocketAddress createInetSocketAddress(String address) {
        return createInetSocketAddress(address, 0);
    }

    /**
     * <p>Creates an InetSocketAddress given a host and optional port in a single String
     * <p/>
     * <p>This allows either IP4 or IP6 addresses (including port) to be provided as Strings as per rfc2732</p>
     *
     * @param address     the address in one of the following formats (the braces '[]'and colon ':' are literal here):
     *                    host<br>
     *                    [host]<br>
     *                    [host]:port<br>
     *                    [host]port<br>
     *                    ip4host:port<br>
     * @param defaultPort The default port to be used ONLY IF the string does not specify a port
     * @see java.net.InetSocketAddress
     */
    public static InetSocketAddress createInetSocketAddress(String address,
            int defaultPort) {
        String original = address.trim();
        String host = original;
        int port = defaultPort;

        if (host.startsWith("[")) {
            // it is an address in [host] or [host]port format
            String[] s = original.split("\\]");
            if (s.length > 1) {
                if (s[1].startsWith(":")) {
                    s[1] = s[1].substring(1);
                }
                port = computePort(s[1], 0);
            }
            host = s[0].substring(1);
        }

        if (host.indexOf(":") == host.lastIndexOf(":")
                && (host.indexOf(":") > -1)) {
            //There is exactly 1 ':' in the string, hence this is an IP4 address which includes a port
            String[] s = original.split("\\:");
            host = s[0];
            if (s.length > 1) {
                port = computePort(s[1], 0);
            }
        }
        return new InetSocketAddress(host, port);
    }

    private static int computePort(String s, int i) {
        try {
            return Integer.valueOf(s);
        } catch (NumberFormatException e) {
            return i;
        }
    }
}

Related Tutorials