实现获取主机名称,IP地址的类 (java)

这是另一个实现获取主机名称,IP地址类.可以获取本机的名称,IP和指定域名的主机名称,IP.返回值为字串型,可以在其它Java类或JSP中引用.  

/**
 *实现获取主机名称,IP地址的类.
 *<br>Copyright (C) ☆苹果工作室☆
 */
package com.applestudio.util;
import java.net.*;
public class Network1{
 InetAddress inet=null;
 
 public static void main( String args[]){
  
  Network1 network=new Network1();
  
  System.out.println("Your host IP is: "+ network.getLIP());
  System.out.println("Your host Name is: "+ network.getLName());
  System.out.println();
  System.out.println("The Server IP is :"+network.getRIP("www.sohu.com"));
  System.out.println("The Server Name is :"+network.getRName("199.181.132.250"));
 }
 
 //取得Localhost的IP地址
 private String getLIP() {
  return getLAddress().getHostAddress();
 }
 
 //取得Localhost的主机名称
 private String getLName() {
  return getLAddress().getHostName();
 }
 
 //取得Remotehost的IP地址
 private String getRIP(String host){
  return getRAddress(host).getHostAddress();
 }
 
 //取得Remotehost的主机名称
 private String getRName(String ip) {
  return getRAddress(ip).getHostName();
 }
 
 private InetAddress getLAddress() {
  try {
   inet=InetAddress.getLocalHost();
  }catch(UnknownHostException e) {}
  return (inet);
 }

 public InetAddress getRAddress(String IP_or_Name){
  try {
   inet=InetAddress.getByName(IP_or_Name);
  }catch (UnknownHostException e) {}
  return (inet);
 }

}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值