怎么通过IP地址获取机器名啊?两边使用socket建立的连接。
在线等,解决马上给分!

解决方案 »

  1.   

    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) {
        }
      

  2.   

    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,这个才是。
      

  3.   

    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);
    }
    }
      

  4.   

    InetAddress i = InetAddress.getByName("192.168.1.1");
    String name=i.getHostName();
    System.out.println(name);
      

  5.   

    楼上两位的早试过了,打印出来的都是IP地址不是机器名,二楼的kidonline(扬帆)不知道怎么试,比如我的IP是192.168.0.2拆成byte类型的数组不知道怎么换算,还请指教!
      

  6.   

    “楼上两位的早试过了,打印出来的都是IP地址不是机器名”
    怎么会?
    InetAddress.getHostName();就是得到机器名