Java Reference
In-Depth Information
The applet
LocalAddress
displays the local host and IP address. Note
that the
SecurityManager
in the browser JVM may block access to this
information. You can try it in the
appletviewer
tool or run it as an
application.
import java.applet.*;
import java.awt.*;
import java.net.*;
/** Show how InetAddress can provide the local
*IPaddress. **/
public class
LocalAddress
extends Applet
{
String fMessage ="";
/**
* Create an instance of InetAddress for the
* local host and display the local IP address.
**/
public void init ()
{
try
{
InetAddress local
-
Address
=
InetAddress.getLocalHost ();
fMessage ="Local IP address ="
+ local
-
Address.toString ();
}
catch (UnknownHostException e) {
fMessage ="Unable to obtain local IP address";
}
System.out.println (fMessage);
} // init
/** Paint IP info in Applet window. **/
public void paint (Graphics g) {
g.drawString (fMessage, 20, 20);
} // paint
/** Print out the local address in app mode. **/
public static void main (String [] args)
{
LocalAddress applet
=
new LocalAddress ();
applet.init ();
}
// main
}
// class LocalAddress
Search WWH ::
Custom Search