InetAddress

// Demonstrate InetAddress.
import java.net.*;
public class InetAddressTest
     public static void main(String args[]) throws UnknownHostException
          InetAddress Address = InetAddress.getLocalHost();
          System.out.println(Address);
          Address = InetAddress.getByName("osborne.com");
          System.out.println(Address);     
          InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
          for(int i = 0; i<SW.length; i++)
               System.out.println(SW);
// I tried to run above program on my Ubuntu 10.11 in eclipse but got following errors
//The method getLocalHost() is undefined for the type InetAddress
//The method getByName(String) is undefined for the type InetAddress
//The method getAllByName(String) is undefined for the type InetAddress
//please tell me where i am wrong .

I suspect you have a class of your own called InetAddress.

Similar Messages

  • InetAddress and hostname containing pause(s)

    Hi!
    I have a problem to get real host name which contains pause. On Windows NT 4.0 is possible to set NETBIOS name containing pause after a lot of warnings :)
    It is not good idea to do so, but if some user do it, I'll have to find a way to understand.
    Can I?
    Simple code as:
    InetAddress localHost = InetAddress.getLocalHost();
    hostName = localHost.getHostName();
    replaces all pauses with '-'.

    What's a "pause"?

  • RMI uses incorrect InetAddress on machine with two nics

    Hello All,
    RMI registry and server run on the same host. Host has two nics, one assigned for private network (192.168.x.x) and another for global.
    For the sake of creating RMI registry I use the global one and specify it as four dot-delimited numbers instead of machine name:
    Registry registry = LocateRegistry.getRegistry("xxx.xxx.xxx.xxx", rmiPort); // "xxx.xxx.xxx.xxx" is global IP addressInvoking a method with just a port as a single parameter causes RMI to bind to the private address, so I avoided that trouble by using numeric IP.
    Remote client is able to locate the registry and extract the stub from it now, but ironically retrieved stub contains private address inside.
    Debugger shows that the proxy stub contains TCPEndpoint which resolves to 192.168.xxx.xxx:yyyy
    I am exporting the object on the server side by using UnicastRemoteObject.exportObject(myObj, 0) method.
    Unfortunately, there is no method which would allow to specify the InetAddress to bind the object on.
    I've seen the battle on page [Remote Method Invocation (RMI) - Why does RMI server sets its connection address?|http://forums.sun.com/thread.jspa?threadID=5366985].
    Close questions are discussed, but I haven't found answer on my question.
    Is there an ability to specify the address when exporting the object?
    Or any workaround to let the client retrieve the proper stub which can be used to send requests to the server?
    Thank,
    Alex.

    For the sake of creating RMI registry I use the global one and specify it as four dot-delimited numbers instead of machine name:
    Registry registry = LocateRegistry.getRegistry("xxx.xxx.xxx.xxx", rmiPort); // "xxx.xxx.xxx.xxx" is global IP address
    That doesn't create a registry. It just returns a stub to an existing registry.
    Invoking a method with just a port as a single parameter causes RMI to bind to the private address.No it doesn't. It causes RMI to construct a stub using "localhost" as the hostname. If this maps to the private address via your /etc/hosts file or DNS, so be it.
    Remote client is able to locate the registry and extract the stub from it now, but ironically retrieved stub contains private address inside.Nothing ironic about it. The stub doesn't know where you got it from. The address inside the stub is controlled by what "localhost" resolves to in the server JVM. If it resolves to something inconvenient, either change that or set the system property java.rmi.server.hostname to the desired IP address or hostname in the server JVM.
    Unfortunately, there is no method which would allow to specify the InetAddress to bind the object on.You don't normally need one, see above, although in pathological cases you could use an RMIServerSocketFactory, which does give you that ability.
    I haven't found answer on my question.See also the RMI FAQ, item A.1.
    Is there an ability to specify the address when exporting the object? See above.

  • Problem when used InetAddress class in Applet

    Hi all.
    I'DongPG from Vietnam.
    I have a question'How to get IP that different from IP default "127.0.0.1" address'.
    I've built an applet where i used InetAddress class to get browser'IP follow:
    InetAddress localIP = InetAddress.getLocalHost();
    String strIP=localIP.getHostAddress();
    I used JBuilder to build applet. It's gotten IP right. Ex:strIP='192.168.100.1'
    But when i used browser link to my Applet in WebServer, it only return strIP='127.0.0.1'
    Thank a lot!

    Wrong forum and cross posted as well.

  • Help - InetAddress.getHostName() returns the ip address

    Hi,
    After updating two windows XP computers witht the latest windows updates the InetAddress.getHostName() stoped returning the remote computers name and instead returns the ip address. This is how it worked :
    rx = a custom DataPacket class
    public InetAddress from;
    System.out.println("From: "+ (rx.from.getHostName() + "/" + rx.from.getHostAddress()) + " " + receivedMessage + "\r\n");
    printed - From: AUTOTEST1/192.1.3.60 test message
    now it prints this - From: 192.1.3.223/192.1.3.223 test message
    Is there an alternate way of getting a computers host name or is there a work around i could implement?
    I need the computers host names as this is a fault logging program and without the computer name the users will not know who they are sending the mesage to as it displays ip addresses and not the computer name
    Thanks,
    sjs1985

    getCanonicalHostName() still returns the ip address instead of the host name, thanx 4 trying to help me.
    It seems like it is not possible to receive the host name on a fully updated Windows XP computer using the InetAddress class anymore.
    I will have to do it the long way and send a message containing the computer name on start up which would then be added to a list of computernames/ip addresses

  • Using InetAddress for ip of local machine

    Hi.
    I'm using InetAddress to obtain the IP address of the local machine. This works fine .. however ... I am trying to simulate a loss of IP address. This is done by using ipconfig/release in DOS. According to DOS, my IP address has now been set to 0.0.0.0, however, InetAddress is still returning the old IP address. My code is as follows :
    InetAddress localHost;
    String localAddress;
    localHost = InetAddress.getLocalHost();
    localAddress = localHost.getLocalAddress();
    Can anyone tell me why this is not now returning 0.0.0.0?
    Thanks,
    Alison Kakoschke.

    I got the same problem. In my case, I want to dynamically detect the IP address of my local machine, which may allocate a new IP address by the DHCP server on the network each time when it is disconnected and then reconnected to the DHCP server. But, the following code:
    String localAddress = InetAddress.getLocalHost().getHostAddress();
    always return the original IP address of the local machine, rather than the new allocated address after disconnected and then reconnected.
    It's emergency! who can help me?!
    please please e-mail to me at: [email protected]

  • InetAddress.getLocalHost() logic with virtual IPs

    Hello.
    I have an application that is using:
    this.serverName = java.net.InetAddress.getLocalHost().getHostName();
    to determine the local machine's hostname. My Solaris box has a physical IP (obviously), and a virtual IP. Here's 'ifconfig -a':
    lo0: flags=849<UP,LOOPBACK,RUNNING,MULTICAST> mtu 8232
    inet 127.0.0.1 netmask ff000000
    hme0: flags=863<UP,BROADCAST,NOTRAILERS,RUNNING,MULTICAST> mtu 1500
    inet 207.240.14.15 netmask ffffff00 broadcast 207.240.14.255
    hme0:1: flags=843<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 207.240.14.16 netmask ffffff00 broadcast 207.240.14.255
    The getLocalHost() call is always returning the IP address of the virtual IP, not the physical. Does anyone happen to know the logic that getLocalHost() uses to determine the IP of the local machine? I sure would think it would pick the physical over any virtuals, but that doesn't seem to be the case.
    I tried the 'last interface brought up' theory, but that didn't pan out either.
    Any help would be appreciated!
    Thanks.

    You may need to get your virutal hostname first.
    String hostn = InetAddress.getByName(hostNm).getHostAddress();

  • InetAddress.getByAddress(byte[]) problem...

    the following line of code is not compiling for me.
    InetAddress inAd = InetAddress.getByAddress(IAbyte);where IAbyte is an byte array of length 4 where each byte is the component of an IP address.
    I am recieving the error:
    C:\tmp\FTPServer,java.java:507: cannot resolve symbol
    symbol : method getByAddress (byte[])
    location: class java.net.InetAddress
                   InetAddress inAd = InetAddress.getByAddress(IAbyte);
    Which is quite odd as the API http://java.sun.com/j2se/1.4.1/docs/api/java/net/InetAddress.html#getByAddress(byte[]) says that the method should be there.
    Knowing me it is just a silly little error on my part ^_^;

    Which is quite odd as the API http://java.sun.com/j2se/1.4.1/docs/api/java/net/InetAddress.html#getByAddress(byte[]) says that the method should be there.
    yes its right
    by the way which version of jdk r u using ...

  • Inetaddress problems

    I need help using Inet address to make a internet usage log viewer. I have found a program to change IP addresses to a site URL. However it is not working correctly. help to rectify this problem will be greatly appreciated.
    Below is the code that i have thus far.
    import java.net.*;
    class Url001{
      public static void main(String[] args){
        try{
          System.out.println(
                  "Get and display IP address of URL by name");
          InetAddress address = InetAddress.getByName(
                                        "www.hotmail.com");
          System.out.println(address);
          System.out.println(
                        "Do reverse lookup on the IP address");
          //Extract the IP address from the string to the right
          // of the /.  Then provide the IP address as a string
          // to the getByName() method.
          int temp = address.toString().indexOf('/');
          address = InetAddress.getByName(
                         address.toString().substring(temp+1));
          System.out.println(address);
          System.out.println(
            "Get and display current IP address of LocalHost");
          address = InetAddress.getLocalHost();
          System.out.println(address);
          System.out.println(
                              "Do reverse lookup on current " +
                                    "IP address of LocalHost");
          temp = address.toString().indexOf('/');
          address = InetAddress.getByName(
                         address.toString().substring(temp+1));
          System.out.println(address);
          System.out.println(
                  "Get and display current name of LocalHost");
          System.out.println(address.getHostName());
          System.out.println(
            "Get and display current IP address of LocalHost");
          //Get IP address as an array of bytes.
          byte[] bytes = address.getAddress();
          //Convert IP address bytes to unsigned values
          // and display separated by spaces.
          for(int cnt = 0; cnt < bytes.length; cnt++){
            int uByte =
                bytes[cnt] < 0 ? bytes[cnt] + 256 : bytes[cnt];
            System.out.print(uByte + " ");
          }//end for loop
          System.out.println();
        }catch(UnknownHostException e){
          System.out.println(e);
          System.out.println("Must be online to run properly.");
        }//end catch
      }//end main
    }//end class Url001 and this is the Expected output
    Get and display IP address of URL by name
    www.hotmail.com/166.63.208.155
    Do reverse lookup on the IP address
    www.hotmail.com/166.63.208.155
    Get and display current IP address of LocalHost
    des-025/10.0.0.63
    Do reverse lookup on current IP address of LocalHost
    des-025/10.0.0.63
    Get and display current name of LocalHost
    des-025.tres-uk.co.uk
    Get and display current IP address of LocalHost
    10 0 0 63
    However this is the output that i am recieving
    Get and display IP address of URL by name
    www.hotmail.com/166.63.208.155
    Do reverse lookup on the IP address
    /166.63.208.155
    Get and display current IP address of LocalHost
    des-025/10.0.0.63
    Do reverse lookup on current IP address of LocalHost
    /10.0.0.63
    Get and display current name of LocalHost
    des-025.tres-uk.co.uk
    Get and display current IP address of LocalHost
    10 0 0 63
    Any help would be greatly recieved, Thank you for your time.
    Regards
    Timothy Jeffreys

    address = InetAddress.getByName(
    address.toString().substring(temp+1));
    System.out.println(address);
    in these lines u r extractin substrin starting from location of " / " (pointed by temp )....
    thats why yr getting this output
    /166.**.**.***
    instead of www.hotmaill.com/166.***.***.***

  • InetAddress.getLocalHost retrieving 127.0.0.1, not the actual IP Address

    I am using the following code :
    try{
              InetAddress iaddr = InetAddress.getLocalHost();
              String ipaddr = iaddr.getHostAddress();
              System.out.println("IP= "+ipaddr);
              }catch(UnknownHostException ex){
                   ex.getMessage();
    to get the IP address of the server on which this code is running.
    We are actually trying to set up different machines (other than the Production machine) for Failover of the Application. So, we need to get the IP address of the machine on which the application is running in the code.
    For one machine, the above code works perfectly OK.
    But, for the three Production machines, the IP address that we are retrieving using the getLocalHost function is fetching the localhost and not the IP address of the machine. That is instead of fetching the IP � a.b.c.d (I am not writing the actual IP in this mail :) ) ... it is fetching the localhost 127.0.0.1 .
    So we need to find out a way to fetch the actual IP address so that failover can work.
    FYI, all of the machines are Solaris machines.
    Regards,
    Anjan

    I am putting up the contents of the /etc/hosts files of the 3 machines I am using.
    @@@@ Machine #1 (Code running OK for this, retrieving a.b.c.155 for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.155 rating-DR loghost
    ==========================
    Host name for Machine #1
    ===================
    bash>hostname
    rating-DR
    @@@@ Machine #2 (Code returning 127.0.0.1 in this server, for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.235 rating1 rating1.xyz.com loghost
    a.b.c.239 rating2
    ==========================
    Hostname for Machine #2
    ===================
    bash>hostname
    rating1
    @@@@ Machine #3 (Code returning 127.0.0.1 in this server, for InetAddress.getLocalhost().getHostAddress()) @@@@
    Excerpt from /etc/hosts file :
    ==========================
    # Internet host table
    127.0.0.1 localhost
    a.b.c.239 rating2 rating2.xyz.com loghost
    192.168.210.235 rating1
    192.168.210.155 rating-DR rating-DR.fastlink.com.jo
    ==========================
    Hostname for Machine #3
    ===================
    bash>hostname
    rating2

  • Converting from a string to InetAddress

    I am reading in a file that contains a line of text, for example:
    192.168.5.2 young
    This line is read in and split up by a string tokenizer. How can I convert the string IP address that is read in as the first token to an actual InetAddress that can be used in network communication.

    I am reading in a file that contains a line of text,
    for example:
    192.168.5.2 young
    This line is read in and split up by a string
    tokenizer. How can I convert the string IP address
    that is read in as the first token to an actual
    InetAddress that can be used in network communication.This code should help you
    byte[] b=new byte[4];
    //how u use getByName
    InetAddress ob1=InetAddress.getByName("abc.xyz.com");
    //how u use getByAddress
    b[0]=new Integer(10).byteValue();
    b[1]=new Integer(200).byteValue();
    b[2]=new Integer(81).byteValue();
    b[3]=new Integer(249).byteValue();
    InetAddress ob2=InetAddress.getByAddress(b);

  • Exception when querying java.lang.InetAddress

    Hi,
    When attempting to query an object based on its ipaddress, I ran into the following problem:
    [java] Exception in thread "main" <4|false|4.0.1> kodo.persistence.ArgumentException: The specified parameter of type "class java.net.Inet4Address" is not a valid query parameter.
    [java] at kodo.jdbc.sql.DBDictionary.setUnknown(DBDictionary.java:1347)
    [java] at kodo.jdbc.sql.SQLBuffer.setParameters(SQLBuffer.java:638)
    [java] at kodo.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:539)
    [java] at kodo.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:512)
    [java] at kodo.jdbc.sql.SelectImpl.execute(SelectImpl.java:332)
    [java] at kodo.jdbc.sql.SelectImpl.execute(SelectImpl.java:301)
    [java] at kodo.jdbc.sql.Union$UnionSelect.execute(Union.java:642)
    [java] at kodo.jdbc.sql.Union.execute(Union.java:326)
    [java] at kodo.jdbc.sql.Union.execute(Union.java:313)
    [java] at kodo.jdbc.kernel.SelectResultObjectProvider.open(SelectResultObjectProvider.java:98)
    [java] at com.solarmetric.rop.EagerResultList.<init>(EagerResultList.java:22)
    [java] at kodo.kernel.QueryImpl.toResult(QueryImpl.java:1445)
    [java] at kodo.kernel.QueryImpl.execute(QueryImpl.java:1167)
    [java] at kodo.kernel.QueryImpl.execute(QueryImpl.java:953)
    [java] at kodo.kernel.QueryImpl.execute(QueryImpl.java:871)
    [java] at kodo.kernel.DelegatingQuery.execute(DelegatingQuery.java:774)
    [java] at kodo.persistence.QueryImpl.execute(QueryImpl.java:241)
    [java] at kodo.persistence.QueryImpl.getResultList(QueryImpl.java:290)
    [java] at test.MainLine.main(MainLine.java:196)
    My query:
    ("select i from Interface i where i.testAddress = addr").setParameter("addr", InetAddress.getLocalHost())
    I have tried this using Inet4Address in the hope that Kodo was having some issues with polymorphism but the same problem occurs.
    The mapping is defined as follows:
    @Basic
    public InetAddress getTestAddress ()
    return _test;
    public void setTestAddress (InetAddress test)
    _test = test;
    I have tried using @Lob but this made no difference. Is it possible to create queries based on a LOB?

    java.sql.SQLException: Invalid state, the Connection object is closed
    It answers your question to an extent. Check if your connection or recordset object is closed
    cheers,
    Siddhs

  • Getting a netative value from InetAddress...

    I have an IP address that I am trying to convert to an InetAddress so I can pick out pieces of it. Unfortunately, when I use the following code, the first region of the IP address is a negative number... Am I doing something incorrectly? (I put in a dummy IP just for the sake of this post, BTW)
    InetAddress address = null;
    try {
    address = InetAddress.getByName("131.255.255.255");
    } catch (UnknownHostException uhe) {
    System.out.println("Unknown Host " + ip);
    System.out.println("address: " + address);
    System.out.println("address.getAddress()[0]: " + (address.getAddress()[0]));
    System.out.println("address.getAddress()[1]: " + (address.getAddress()[1]));
    System.out.println("address.getAddress()[2]: " + (address.getAddress()[2]));
    System.out.println("address.getAddress()[3]: " + (address.getAddress()[3]));
    This is the output:
    address: 131.255.255.255/131.255.255.255
    address.getAddress()[0]: -125
    address.getAddress()[1]: 255
    address.getAddress()[2]: 255
    address.getAddress()[3]: 255
    Why is this showing up as -125 and not 131?

    0000 .... 1000 0011 = 131 int
    cut to byte
    1000 0011 = -125
    prolly recast to int to get the right value...sorry, you know what I mean, not cast, since that'd keep the sign, put the 0's back

  • Problem with InetAddress.getByName ("......" ).getHostName(); Help !

    basic problem is that IP does not get resolved under Jdk 1.4 SuSE 7.3
    the following code resolves IP under jdk 1.3 on the same machine, BUT doesn't resolve under Jdk 1.4 on the same machine.
    public class MyTest
         public static void main( String []argv )
              try{
              InetAddress addr = InetAddress.getByName ("64.68.82.30" );               
              String hostName = addr.getHostName();
              System.out.println( hostName );
              }catch( Exception e ){
                   e.printStackTrace();
    hovewer we found that it's not a problem of JDK 1.4 as we have ANOTHER machine with 1.3 and 1.4 running on it where ip gets resolved with the above code.
    maybe some configuration problem ... if so - where and what to configure ...

    more info:
    does not resolve under jdk 1.4 means: it returns the same IP which was passed as a parameter to InetAddress.getByName(). NO exception is thrown.
    reminding: same code on same machine resolves that IP under jdk 1.3

  • InetAddress Question

    I have a small applet that I want to return
    the local computer's hostname.
    It works, but when I leave the page and return
    I get the local loop back as my IP and an exception is throw.
    Why is my computer's port being locked by my first call to InetAddress.getLocalHost(). Is there a way to free my broswer or applet ?

    Its in the start() method.
    I have two <object> tags in my html, that both call the same class file, one of them has a name attribute.
    I do an OnLoad in Javascript using the 'name' of the second <object> to call a method (getvalue())to return the value of the IP to the broswer.
    The Javacode is
    public class GetClientIP extends Applet {
    static public String mystring;
    public String getvalue() {
    return mystring;
    public void start {
    try { InetAddress getIP = InetAddress.getLocalHost();
    mystring += InetAddress.getLocalHost();
    catch (Exception e) {
    e.printStacktace(); }
    }

  • InetAddress UnknownHostException

    Hello,
    InetAddress.getLocalHost() is throwing a UnknownHostException when i'm running an app from within another app using Runtime.exec() and the command string "cmd /k appname". However it returns the hostname fine when the app is run from the command prompt!
    I'm guessing it's a environment veraiable or another configuration problem but have not been able to figure of what to missing so far.
    Any help would be appreicated

    Fixed the problem,
    requires SystemRoot to be set

Maybe you are looking for

  • Upgrading to LR 3.2 from 2.6 - Should I use the LR 3 box?

    As I intend to upgrade to Lightroom 3.2 from 2.6, I wonder if I should use the upgrade box or donload a trial version and insert the key to be found in that box. There was a thread lelated to migration from LR 1.4 to 2.2 ( http://forums.adobe.com/mes

  • If i have charges on my account that i did not purchase. How do i dispute them with I Tunes?

    Hello

  • IPhone 3GS crashes with iTunes 9.0.2

    Hello, I have a iPhone 3GS with latest 3.1.2 firmware and on my Macbook Pro I have iTunes 9.0.2 installed. Every time I connect my iPhone to my Macbook and load iTunes, iTunes just seems to hang with the spinning beachball and says in the dock applic

  • Searching portal content

    Hi all, I would like to configure TREX to search the URL iViews are accessible by the current user and display the results. The links in the results should navigate back to the corresponding iview in the content area. In other words the TLN and detai

  • Return to run the program again

    Hello all! I'm wondering if it is possible to return to the last step of the program to run it again in labview. For example, a vi doing computation, if the final result doesn't satisfy a certain standard, I would like that a one button diagram shows