InetAddress

L

Linus

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
 
S

Steve Horsley

Linus said:
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
 
A

Andrew Tyson

Linus said:
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 ....
 
N

Nick

Linus said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top