java Socket 获取本地主机ip

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

}

posted @ 2013-11-26 11:24  剑握在手  阅读(1693)  评论(0编辑  收藏  举报
返回顶部↑