Get Correct IP Address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesti
    New Member
    • Nov 2007
    • 144

    Get Correct IP Address

    hello,

    i'm developing a J2SE VoIP app. i used the InetAddress.get LocalHost() to get the address of the local machine.

    it works in xp, but not in Ubuntu. in Ubuntu it gives me "127.0.1.1" , which is listed in /etc/hosts file.

    after some search, some advice to use

    Code:
    NetworkInterface.getNetworkInterfaces();
    to get list of the available interfaces. but when i println

    Code:
    NetworkInterface iface = en.nextElement();
    System.err.println(iface.getInterfaceAddresses());
    i get this output

    Code:
    [/fe80:0:0:0:216:36ff:feac:d855%2/64 [null], /192.168.1.2/24 [/192.168.1.255]]
    the mac address, then the ip address. how to get only the "192.168.1. 2" part?

    i've tried using
    Code:
    iface.getInetAddresses()
    but i get the following output
    Code:
    java.net.NetworkInterface$1checkedAddresses@1884a40
    another consideration is, which interface address should i use? i can check if it's a loopback interface since NetworkInterfac e has the isLoopback method.

    but sometimes, a machine has two or more NIC with one connected to the local network and the other one to the internet. if the user chooses to connect to a local server, i should choose the interface connected the LAN. and the same thing apply if the user wants to connect to a public server in the internet, i should choose the address connected to the internet.

    how to determine this?

    Thank you.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by thesti
    Code:
    [/fe80:0:0:0:216:36ff:feac:d855%2/64 [null], /192.168.1.2/24 [/192.168.1.255]]
    the mac address, then the ip address. how to get only the "192.168.1. 2" part?
    Why not use a regular expression to grab it from this String?

    Comment

    Working...