Java Host Check normalizeHostName(String name)

Here you can find the source of normalizeHostName(String name)

Description

Given a string representation of a host, return its ip address in textual presentation.

License

Apache License

Parameter

Parameter Description
name a string representation of a host: either a textual representation its IP address or its host name

Return

its IP address in the string format

Declaration

public static String normalizeHostName(String name) 

Method Source Code

//package com.java2s;
/**/*from  w w w . j a  va 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.UnknownHostException;

public class Main {
    /**
     * Given a string representation of a host, return its ip address
     * in textual presentation.
     *
     * @param name a string representation of a host:
     *             either a textual representation its IP address or its host name
     * @return its IP address in the string format
     */
    public static String normalizeHostName(String name) {
        try {
            return InetAddress.getByName(name).getHostAddress();
        } catch (UnknownHostException e) {
            return name;
        }
    }
}

Related

  1. isInternalHostname(String host)
  2. isLocal(final String hostName)
  3. isLocal(final String hostname)
  4. isThisMe(String hostname)
  5. normalizeHost(String host)
  6. normalizeHostNames(Collection names)