Friday, February 14, 2014

Get address of local host with InetAddress.getLocalHost()


  • InetAddress.getLocalHost() returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
InetAddress.getLocalHost()
InetAddress.getLocalHost()

package javaexnetworking;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaExNetworking {
    
    public static void main(String[] args) {
        try {
            InetAddress localHostAddress = InetAddress.getLocalHost();
            System.out.println(localHostAddress);
        } catch (UnknownHostException ex) {
            Logger.getLogger(JavaExNetworking.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
}

No comments:

Post a Comment