怎么通过IP地址获取机器名啊? 怎么通过IP地址获取机器名啊?两边使用socket建立的连接。在线等,解决马上给分! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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) { } try { // 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) { }呵呵,刚才看错了,上面是从hostname获得ip,这个才是。 import java.net.*;public class TestHost { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub InetAddress i = InetAddress.getByName("192.168.0.1"); String s=i.getHostName(); System.out.println(s); }} InetAddress i = InetAddress.getByName("192.168.1.1");String name=i.getHostName();System.out.println(name); 楼上两位的早试过了,打印出来的都是IP地址不是机器名,二楼的kidonline(扬帆)不知道怎么试,比如我的IP是192.168.0.2拆成byte类型的数组不知道怎么换算,还请指教! “楼上两位的早试过了,打印出来的都是IP地址不是机器名”怎么会?InetAddress.getHostName();就是得到机器名 XML形式的字符串怎么转换成xml文件 寻求公司实习,请网友帮忙! 请问这个程序错在哪里? 刚开始学JSP,请教各位一个jsp问题 java数组的问题 一个菜鸟问题,希望有高手解答! 读文件问题? 在线等待,猜字谜语 对于初学者应该看什么样的java书籍 我编了一个检测ip地址的程序,可是遇到代理服务器就不灵了,请问如何在程序中加以改变?? 如何在if里先赋值再判断布尔值? 关于java程序运行后报:"Unexpected Signal"的问题,哪位大虾知道解决办法呢?头疼~~~~~
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) {
}
// 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) {
}
呵呵,刚才看错了,上面是从hostname获得ip,这个才是。
public class TestHost { /**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
InetAddress i = InetAddress.getByName("192.168.0.1");
String s=i.getHostName();
System.out.println(s);
}
}
String name=i.getHostName();
System.out.println(name);
怎么会?
InetAddress.getHostName();就是得到机器名