Java Socket Address Get parseInetSocketAddress(String endPoint)

Here you can find the source of parseInetSocketAddress(String endPoint)

Description

Parse an InetSocketAddress object the given string.

License

Apache License

Parameter

Parameter Description
endPoint the string to parse.

Return

the parsed from string.

Declaration

public static InetSocketAddress parseInetSocketAddress(String endPoint) 

Method Source Code


//package com.java2s;
/*/*from  w w  w .  j a  v a 2 s  .  c o  m*/
 * Copyright 2002-2007 the original author or authors.
 *
 * 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.
 */

import java.net.InetSocketAddress;

public class Main {
    /**
     * Parse an {@link InetSocketAddress} object the given string. The
     * <code>endPoint</code> is in format of <code>[hostname:]port</code>.
     * It can be either a port number, or a hostname and port number 
     * separated by the character ':'.<p>
     * 
     * For example, '<code>www.company.com:80</code>', '<code>1234</code>'
     * and '<code>localhost:3344</code>' are all valid end points.
     * 
     * @param endPoint  the string to parse.
     * @return  the {@linkplain InetSocketAddress} parsed from string.
     * @exception  NumberFormatException  if the port number is not a integer.
     */
    public static InetSocketAddress parseInetSocketAddress(String endPoint) {
        String hostname = null;
        String portString;
        int port;

        int index = endPoint.indexOf(":");
        if (index >= 0) {
            hostname = endPoint.substring(0, index);
            portString = endPoint.substring(index + 1);
        } else {
            portString = endPoint;
        }
        port = Integer.parseInt(portString);
        return hostname == null ? new InetSocketAddress(port) : new InetSocketAddress(hostname, port);
    }
}

Related

  1. isUnresolved(SocketAddress address)
  2. isValidMulticastAddress(final InetSocketAddress address)
  3. normalizeSocketAddr(InetSocketAddress address)
  4. parseAddress(SocketAddress address)
  5. parseInetSocketAddress(String address)
  6. parseInetSocketAddress(String text)
  7. parseSocketAddress(String address)
  8. parseSocketAddress(String addressPort, int defaultPort)
  9. parseSocketAddress(String addrString)