import java.net.*;
public class test {
public static InetAddress ina;
public static void main(String [] args){
try{
ina = InetAddress.getByName("sharon");
System.out.println(ina.getHostAddress());
}
catch(Exception e){
System.out.println(e);
}
}
}
其中sharon是我的机器名,换成你的机器名就可以了。
public class test {
public static InetAddress ina;
public static void main(String [] args){
try{
ina = InetAddress.getByName("sharon");
System.out.println(ina.getHostAddress());
}
catch(Exception e){
System.out.println(e);
}
}
}
其中sharon是我的机器名,换成你的机器名就可以了。
import java.net.InetAddress;public class getIp { static void getIp() {
try{
InetAddress Ipaddress = InetAddress.getLocalHost();
System.out.println(Ipaddress.toString());
}
catch(Exception e){
System.out.println(e);
}
} public static void main(String [] args){
getIp(); }}
// Get hostname by textual representation of IP address
InetAddress addr = InetAddress.getByName("127.0.0.1");
// Get hostname by a byte array containing the IP address
byte[] ipAddr = new byte[]{127, 0, 0, 1};
addr = InetAddress.getByAddress(ipAddr);
// Get the host name
String hostname = addr.getHostName();
// Get canonical host name
String hostnameCanonical = addr.getCanonicalHostName();
} catch (UnknownHostException e) {
}
--------------------------------------------------------------
try {
InetAddress addr = InetAddress.getByName("javaalmanac.com");
byte[] ipAddr = addr.getAddress();
// Convert to dot representation
String ipAddrStr = "";
for (int i=0; i<ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i]&0xFF;
}
} catch (UnknownHostException e) {
}