Java,获取子网掩码
昨天写代码写到一半,突然想到,之前测试的时候都是手动输入子网掩码的,因而想着如何自动获得子网掩码,于是就google了下,发现确实是有这方面的东西,可能我是自己比较笨,看不懂别人的代码,也因为找到的代码很少注释(这可能是中国人写程序的习惯),所以我就不想再看了,但是多少还是有点启发的,查看了jdk以后,知道可以通过NetworkInterface、InterfaceAddress这两个类来实现,可以获得子网掩码前缀,然后通过运算就能得到子网掩码,要说明的是,我计算掩码的方法很土但是很好用,代码也很容易看的懂,还有一个要说明的是,我是xp系统,没有安装ipv6协议,因为安装了ipv6后会有多个结果
代码:
public class SubnetMask {
/**
* @param args
*/
public static String getSubnetMask(){
int prefix=0;
int[] ipSplit=new int[4];
String subnetMask=null;
InetAddress localMachine=null;
try {
localMachine=InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NetworkInterface netCard=null;
try {
netCard=NetworkInterface.getByInetAddress(localMachine);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<InterfaceAddress> localInterface=null;
localInterface=netCard.getInterfaceAddresses();
Iterator<InterfaceAddress> iterator=null;
iterator=localInterface.iterator();
while(iterator.hasNext()){
InterfaceAddress temp=null;
temp=iterator.next();
prefix=temp.getNetworkPrefixLength();
}
int index=0;
int split=0;
int remainder=0;
split=prefix/8;
remainder=prefix%8;
while(index<split){
ipSplit[index]=255;
index++;
}
if(remainder==1)
ipSplit[index]=128;
if(remainder==2)
ipSplit[index]=192;
if(remainder==3)
ipSplit[index]=224;
if(remainder==4)
ipSplit[index]=240;
if(remainder==5)
ipSplit[index]=248;
if(remainder==6)
ipSplit[index]=252;
if(remainder==7)
ipSplit[index]=254;
index++;
相关文档:
先来了解一下链表模式的原理:
首先写一个JavaBean,内容是要添加的元素和该元素的节点。
public class NodeBean implements Serializable
{
private Object data; //元素本身
private NodeBean next; //下一个节点
&n ......
级别: 初级
唐
咸峰
(hutun@263.net
),
2001 年 5 月 28 日
随着JAVA的迅猛发展,JDK版本的不断更
新,JAVA新的事件模型与旧的JDK模型也有了本质的区别,它的事件模型也有了很大的区别。由于现在的编程都是采用事件驱动,所以很有必要了解的事件模
型,我们下面从定制事件的实际出发来具体讨论。
问题的提出
......
For Ubuntu 10.04 LTS, the sun-java6 packages have been dropped from the Multiverse section of the Ubuntu archive. It is recommended that you use openjdk-6 instead.
If you can not switch from the proprietary Sun JDK/JRE to OpenJDK, you can install sun-java6 packages from the Canonical Partner Reposi ......
在很多时候我们需要将一个给了完整路径的类对象字符串转换成一个类的实例对象,就比如说有以下这样的一个字符串
String menAction= "gef.putin.step.ui.SystemMenuTest(xiajiaji)"在该类中需要有有一个字符串格式的参数,当然多个参数也可以,我已经将多个参数的考虑进去,对改字符串进行解析就可以转换成类的实例对 ......
-------------------------------oracle--------------------------------
驱动:oracle.jdbc.driver.OracleDriver
URL:jdbc:oracle:thin:@<machine_name><:port>:dbname
注:machine_name:数据库所在的机器的名称;
port:端口号,默认是1521
&nbs ......