java Socket 獲取本地主機ip

劍握在手發表於2013-11-26

package cn.itcast.net.p1.ip;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPDemo {

 /**
  * @param args
  * @throws UnknownHostException
  */
 public static void main(String[] args) throws UnknownHostException {

  //獲取本地主機ip地址物件。
  InetAddress ip = InetAddress.getLocalHost();
  
  //獲取其他主機的ip地址物件。
  ip = InetAddress.getByName("192.168.1.110");//InetAddress.getByName("my_think");
  
  System.out.println(ip.getHostAddress());
  System.out.println(ip.getHostName());
 }

}

相關文章