InetAddress

Discussion in 'Java' started by Linus, Jan 28, 2005.

  1. Linus

    Linus Guest

    I don't understand how to set a InetAddress variable with an IP address like

    InetAddress ip = 192.168.0.1;

    I thik it's some kind of syntax error.
    Thanks to everyone who'll help me.

    Linus
     
    Linus, Jan 28, 2005
    #1
    1. Advertising

  2. wrote in comp.lang.java.programmer:
    > I don't understand how to set a InetAddress variable with an IP address like
    >
    > InetAddress ip = 192.168.0.1;
    >
    > I thik it's some kind of syntax error.


    There is a fundamental syntax error in your code. It is such an
    error, that it might be a better idea for you to start from the
    Java tutorial: <http://java.sun.com/docs/books/tutorial/>.

    --
    Antti S. Brax Rullalautailu pitää lapset poissa ladulta
    http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/
    "Disconnect this cable to shorten, re-connect to lengthen."
    -- Instructions on Logitech's USB mouse extension cord.
     
    Antti S. Brax, Jan 28, 2005
    #2
    1. Advertising

  3. Linus wrote:
    > I don't understand how to set a InetAddress variable with an IP address like
    >
    > InetAddress ip = 192.168.0.1;
    >
    > I thik it's some kind of syntax error.
    > Thanks to everyone who'll help me.
    >
    > Linus


    It has no public constructor, so you have to call one of InetAddress's
    static factory methods (a method that makes instances of InetAddress).
    In this case, I think you want:

    InetAddress ia = InetAddress.getByName("192.168.0.1");

    Steve
     
    Steve Horsley, Jan 28, 2005
    #3
  4. Linus

    Andrew Tyson Guest

    Linus wrote:
    > InetAddress ip = 192.168.0.1;


    A little off topic but making this syntax legal would be a nice
    enhancement of the JDK 1.5 autoboxing facility IMHO ....
     
    Andrew Tyson, Jan 29, 2005
    #4
  5. Linus

    Nick Guest

    Linus wrote:
    > I don't understand how to set a InetAddress variable with an IP address like
    >
    > InetAddress ip = 192.168.0.1;
    >
    > I thik it's some kind of syntax error.
    > Thanks to everyone who'll help me.
    >
    > Linus

    The InetAddress class is a little unusual in that it doesn't have any
    public constructors. Instead you pass the host name or string format of
    the dotted quad address to the static InetAddress.getByName() method
    like this:

    try {
    InetAddress utopia = InetAddress.getByName("utopia.poly.edu");
    InetAddress duke = InetAddress.getByName("128.238.2.92");
    }
    catch (UnknownHostException ex) {
    System.err.println(ex);
    }

    -Nick
     
    Nick, Jan 29, 2005
    #5
  6. Linus

    Linus Guest

    Thank you to everyone, you have resolved my problem.

    Linus
     
    Linus, Jan 31, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C
    Replies:
    1
    Views:
    860
  2. vertigo

    IP: long-> InetAddress

    vertigo, May 2, 2004, in forum: Java
    Replies:
    2
    Views:
    4,498
    Roedy Green
    May 3, 2004
  3. Madhur Ahuja
    Replies:
    1
    Views:
    793
    Paul Lutus
    Aug 29, 2004
  4. Replies:
    0
    Views:
    4,651
  5. Rhino

    InetAddress question

    Rhino, Feb 26, 2005, in forum: Java
    Replies:
    1
    Views:
    468
    Tilman Bohn
    Feb 26, 2005
Loading...

Share This Page