集册 Java实例教程 获取IP地址的主机名

获取IP地址的主机名

欢马劈雪     最近更新时间:2020-01-02 10:19:05

419
获取IP地址的主机名

import java.net.InetAddress;

import java.net.UnknownHostException;


public class Main {/**from 时代Java公众号 - N o w J a  v a . c o m**/

  public void m() {

    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

   
展开阅读全文