Java Socket Address Get getInetSocketAddress(String endpointURI, int defaultPort)

Here you can find the source of getInetSocketAddress(String endpointURI, int defaultPort)

Description

get Inet Socket Address

License

Apache License

Declaration

public static InetSocketAddress getInetSocketAddress(String endpointURI, int defaultPort) 

Method Source Code

//package com.java2s;
/*/*ww w  .  ja v  a  2  s  .  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.net.InetAddress;
import java.net.InetSocketAddress;

import java.net.UnknownHostException;

public class Main {
    public static InetSocketAddress getInetSocketAddress(String endpointURI, int defaultPort) {
        final int pos = endpointURI.indexOf(':');
        if (pos == -1) {
            InetAddress addr = getInetAddress(endpointURI);
            return new InetSocketAddress(addr, defaultPort);
        } else {
            String host = endpointURI.substring(0, pos);
            InetAddress addr = getInetAddress(host);
            String portStr = endpointURI.substring(pos + 1);
            int port = Integer.parseInt(portStr);
            return new InetSocketAddress(addr, port);
        }
    }

    public static InetAddress getInetAddress(final String addressOrName) {
        try {
            return InetAddress.getByName(addressOrName);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("Cannot find InetAddress: " + addressOrName);
        }
    }
}

Related

  1. getHostStringWithoutNameLookup( InetSocketAddress inetSocketAddress)
  2. getInetSocketAddress(long addrAsInt32, int port)
  3. getInetSocketAddress(String addressPortStr, int defaultPort)
  4. getInetSocketAddress(String arg)
  5. getInetSocketAddress(String endpoint)
  6. getInetSocketAddress(String s, String mainHost)
  7. getInetSocketAddressFromStringStrict(String s)
  8. getInetSocketTransportAddressConstructor()
  9. getIp(SocketAddress socketAddress)