1 public static void main (string[] args) {2 try {3 inetaddress address = Inetaddress.getlocalhos t ();//Gets the local IP address//pc-20140317pxkx/192.168.0.121 4 String hostaddress = address.gethostaddress ());//192.168. 0.121 5 InetAddress Address1 = Inetaddress.getbyname ("www.wodexiangce.cn");//Gets the IP address of the website, such as all of our Requests are through Nginx, so here is actually the Nginx server IP Ground 6 String hostAddress1 = address1.gethostaddress ());//124.237.121.122 7 Inetaddress[] addresses = Inetaddress.getallbyname ("www.baidu.com");//returns all of its possible inetaddress objects based on hostname 8 for (InetAddress addr:addresses) {9 System.out.println (addr);//www.baidu.com/14.215.177.38 10 www.baidu.com/14.215.177.37 One} of catch (Unknownhostexception e) {E.printstack Trace (); +}
/**
* Get server IP Address
* @return
*/
@SuppressWarnings ("Unchecked")
public static String Getserverip () {
String server_ip = null;
try {
Enumeration netinterfaces = Networkinterface.getnetworkinterfaces ();
inetaddress IP = null;
while (Netinterfaces.hasmoreelements ()) {
NetworkInterface ni = (networkinterface) netinterfaces.nextelement ();
ip = (inetaddress) ni.getinetaddresses (). Nextelement ();
Server_ip = Ip.gethostaddress ();
if (!ip.issitelocaladdress () &&!ip.isloopbackaddress ()
&& ip.gethostaddress (). IndexOf (":") = =-1) {
Server_ip = Ip.gethostaddress ();
Break
} else {
IP = null;
}
}
} catch (SocketException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return server_ip;
}
}
Java gets the local IP address