Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getLocalHost();

    // Get IP Address
    byte[] ipAddr = addr.getAddress();

    // Get hostname
    String hostname = addr.getHostName();
}

From source file:Main.java

public static void main(String[] args) {

    try {//from   w w w  .j  a  va2 s.  c  o m
        InetAddress me = InetAddress.getLocalHost();
        String dottedQuad = me.getHostAddress();
        System.out.println("My address is " + dottedQuad);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InetAddress add = InetAddress.getLocalHost();

    System.setProperty("java.security.policy", "file:/C:/java.policy");
    SecurityManager sm = new Main();
    System.setSecurityManager(sm);

    sm.checkMulticast(add);/*from   w ww.  j  av a 2 s . c o  m*/

    System.out.println("Allowed!");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getLocalHost();

    NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    byte[] mac = ni.getHardwareAddress();

    for (int i = 0; i < mac.length; i++) {
        System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
    }/*from w ww .j  a v a 2s  .co m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress ip = InetAddress.getLocalHost();
    System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    System.out.print("Current MAC address : ");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
    }//w  w  w  .  j a v  a 2 s  .  c  o  m
    System.out.println(sb.toString());
}

From source file:Main.java

public static void main(String[] args) {

    try {/* w  w w  .jav  a  2 s. c o  m*/
        InetAddress address = InetAddress.getLocalHost();
        System.out.println("My name is " + address.getHostName());
    } catch (UnknownHostException e) {
        System.out.println("I'm sorry. I don't know my own name.");
    }

}

From source file:MainClass.java

public static void main(String[] args) {

    try {//  w w w .  ja  v a  2  s . c om
        InetAddress me = InetAddress.getLocalHost();
        String dottedQuad = me.getHostAddress();
        System.out.println("My address is " + dottedQuad);
    } catch (UnknownHostException e) {
        System.out.println("I'm sorry. I don't know my own address.");
    }

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    InetAddress myAddress = InetAddress.getLocalHost();
    System.out.println(myAddress);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress dst = InetAddress.getLocalHost();
    int port = 8080;
    byte[] outbuf = new byte[1024];
    int len = 1024;

    DatagramPacket request = new DatagramPacket(outbuf, len, dst, port);
    DatagramSocket socket = new DatagramSocket();
    socket.send(request);//  w ww  .  j  a va2  s .  c  o m
}

From source file:DNSLookup.java

public static void main(String args[]) {
    try {/*  w  w  w.  j ava 2s . co m*/
        InetAddress host;
        if (args.length == 0) {
            host = InetAddress.getLocalHost();
        } else {
            host = InetAddress.getByName(args[0]);
        }
        System.out.println("Host:'" + host.getHostName() + "' has address: " + host.getHostAddress());
        byte bytes[] = host.getAddress();
        int fourBytes[] = new int[bytes.length];
        for (int i = 0, n = bytes.length; i < n; i++) {
            fourBytes[i] = bytes[i] & 255;
        }
        System.out.println("\t" + fourBytes[0] + "." + fourBytes[1] + "." + fourBytes[2] + "." + fourBytes[3]);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}