Get local IP addresses and host names

xiaoxiao2021-03-06  46

Get local IP addresses and hostnames [Repost]

/ * Filename: iPhomeWork.java Author: Wandering Kid Date: 2004-7-5 E-mail: qiyadeng@hotmail.com

PURPOSE: Get the IP address and host name of the local host * /

Import java.net.inetaddress;

Import java.net.unknownhostexception;

Public class iphomework {

Inetaddress addr = null;

Byte [] ipaddr = NULL;

String hostname = "";

String ipaddress = "";

Public static void main (string arg []) {iphomework ip = new iphomework ();

System.out.println ("The localhost's ip is:" ip.getlocalhostip ());

System.out.println ("The localhost's hostname is:" ip.getlocalhostname ());

}

Public String getLocalHostip ()

{

Try {addr = inetaddress.getlocalhost ();

ipaddr = addr.getaddress ();

For (int i = 0; i

0)

{ipaddress = ".";

ipaddress = ipaddr [i] & 0xff;

}

} catch (unknownhostexception e) {}

Return ipaddress;

}

Public string getlocalhostname () {

Try {

AddR = inetaddress.getlocalhost ();

Hostname = addr.gethostname ();

} catch (unknownhostexception e) {}

Return Hostname;

}

}

转载请注明原文地址:https://www.9cbs.com/read-80016.html

New Post(0)