导航:首页 > 编程语言 > inetaddressjava

inetaddressjava

发布时间:2024-11-21 06:26:41

java 如何获取连接网络连接信息

用类java.net.InetAddress中
byte[] getAddress() 返回此 InetAddress 对象的原始 IP 地址。
static InetAddress[] getAllByName(String host) 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。
static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
String getCanonicalHostName() 获取此 IP 地址的完全限定域名。
String getHostAddress() 返回 IP 地址字符串(以文本表现形式)。
String getHostName() 获取此 IP 地址的主机名。
static InetAddress getLocalHost() 返回本地主机。
Enumeration netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();

while (netInterfaces.hasMoreElements()) {

NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());

Enumeration ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:"
+ ips.nextElement().getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}

㈡ java 怎么根据IP地址获取主机名

//看看这个代码如何。
importjava.net.InetAddress;
importjava.net.UnknownHostException;
importjava.util.Properties;
importjava.util.Set;


{

publicstaticvoidmain(String[]args){
InetAddressnetAddress=getInetAddress();
System.out.println("hostip:"+getHostIp(netAddress));
System.out.println("hostname:"+getHostName(netAddress));
Propertiesproperties=System.getProperties();
Set<String>set=properties.stringPropertyNames();//获取java虚拟机和系统的信息。
for(Stringname:set){
System.out.println(name+":"+properties.getProperty(name));
}
}

(){

try{
returnInetAddress.getLocalHost();
}catch(UnknownHostExceptione){
System.out.println("unknownhost!");
}
returnnull;

}

publicstaticStringgetHostIp(InetAddressnetAddress){
if(null==netAddress){
returnnull;
}
Stringip=netAddress.getHostAddress();//gettheipaddress
returnip;
}

publicstaticStringgetHostName(InetAddressnetAddress){
if(null==netAddress){
returnnull;
}
Stringname=netAddress.getHostName();//getthehostaddress
returnname;
}

}

这个代码简单明了,就是调用现成的InetAddress类

㈢ java中InetAddress的getAddress和getHostAddress有什么区别

getHostAddress为byte数组,getAddress是个String字符串。
所以,getAddress方便展示,getHostAddress方便作为数据进行处理。

阅读全文

与inetaddressjava相关的资料

热点内容
网络中常用的传输介质 浏览:518
文件如何使用 浏览:322
同步推密码找回 浏览:865
乐高怎么才能用电脑编程序 浏览:65
本机qq文件为什么找不到 浏览:264
安卓qq空间免升级 浏览:490
linux如何删除模块驱动程序 浏览:193
at89c51c程序 浏览:329
怎么创建word大纲文件 浏览:622
袅袅朗诵文件生成器 浏览:626
1054件文件是多少gb 浏览:371
高州禁养区内能养猪多少头的文件 浏览:927
win8ico文件 浏览:949
仁和数控怎么编程 浏览:381
项目文件夹图片 浏览:87
怎么在东芝电视安装app 浏览:954
plc显示数字怎么编程 浏览:439
如何辨别假网站 浏览:711
宽带用别人的账号密码 浏览:556
新app如何占有市场 浏览:42

友情链接