Java Reference
In-Depth Information
Fig. 2.1
Using method getByName to retrieve IP address of a specifi ed host
It is sometimes useful for Java programs to be able to retrieve the IP address of
the current machine. The example below shows how to do this.
Example
import java.net.*;
public class MyLocalIPAddress
{
public static void main(String[] args)
{
try
{
InetAddress address =
InetAddress.getLocalHost();
System.out.println(address);
}
catch (UnknownHostException uhEx)
{
System.out.println(
"Could not fi nd local address!");
}
}
}
Output from this program when run on the author's offi ce machine is shown in
Fig. 2.2 .
Search WWH ::




Custom Search