代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
如何通过Java的类库获取主机IP地址
作者:
Dezai.CN
/ 发布于
2011/12/1
/
747
<div> public class MyTest { public static void main(String[] args) { //1. 通过NetworkInterface类枚举出当前主机的全部网络设备数据 try { //1.1 返回当前主机每一个网络接口对应的实例 Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces(); if (interfaceList == null) { System.out.println("--No interfaces found--"); } else { while (interfaceList.hasMoreElements()) { NetworkInterface iface = interfaceList.nextElement(); //1.2 iface.getName()方法返回的是设备的名称,通常由字母和数字联合构成。 System.out.println("Interface " + iface.getName() + ":"); //1.3 枚举出真正持有IP地址信息的对象 Enumeration<InetAddress> addrList = iface.getInetAddresses(); if (!addrList.hasMoreElements()) System.out.println("\t(No Addresses for this interface."); while (addrList.hasMoreElements()) { InetAddress addr = addrList.nextElement(); //1.4 这里输出的就是我们通常所说的点分形式的IP地址,如192.163.1.1 if (addr instanceof Inet4Address) System.out.println("\tAddress " + addr.getHostAddress()); } } } } catch (SocketException e) { System.out.println("Error getting network interfaces: " + e.getMessage()); } //2. 根据指定主机名(host),获取该主机的全部网络设备信息。 String host = "Stephen-PC"; try { System.out.println(host + ":"); //2.1 通过主机名称直接获取该主机的全部网络地址。 InetAddress[] addrList = InetAddress.getAllByName(host); for (InetAddress addr : addrList) { //2.2 getHostName()获取的是主机名,getHostAddress()获取的是点分形式的IP地址。 if (addr instanceof Inet4Address) System.out.println("\t" + addr.getHostName() + "/" + addr.getHostAddress()); } } catch (UnknownHostException e) { System.out.println("\tUnable to find address for " + host); } } } /* 输出结果如下: Interface lo: Address 127.0.0.1 Interface net0: (No Addresses for this interface. Interface net1: (No Addresses for this interface. Interface net2: (No Addresses for this interface. Interface net3: (No Addresses for this interface. Interface ppp0: (No Addresses for this interface. Interface eth0: (No Addresses for this interface. Interface eth1: (No Addresses for this interface. Interface eth2: (No Addresses for this interface. Interface ppp1: (No Addresses for this interface. Interface net4: (No Addresses for this interface. Interface eth3: Address 10.24.194.17 Interface net5: (No Addresses for this interface. Interface net6: (No Addresses for this interface. Interface eth4: Interface net7: (No Addresses for this interface. Interface net8: (No Addresses for this interface. Interface eth5: Address 192.168.225.1 Interface eth6: Address 192.168.220.1 Interface eth7: (No Addresses for this interface. Interface eth8: (No Addresses for this interface. Interface eth9: (No Addresses for this interface. Interface eth10: (No Addresses for this interface. Interface eth11: (No Addresses for this interface. Stephen-PC: Stephen-PC/10.24.194.17 Stephen-PC/192.168.225.1 Stephen-PC/192.168.220.1 */</div>
试试其它关键字
获取主机IP地址
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3