用split(“.”)将字符串分割成四段,再转换成数字,最后转换成二进制串,就是ip.。如果不够四段或者不是数字等等就是错误
创新互联建站服务项目包括台江网站建设、台江网站制作、台江网页制作以及台江网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,台江网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到台江省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
//获取ip地址
public String getLocalIpAddress()
{
try {
String ipv4;
ArrayListNetworkInterface nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist)
{
ArrayListInetAddress ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist)
{
if (!address.isLoopbackAddress() InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))
{
return ipv4;
}
}
}
} catch (SocketException ex)
{
//Log.e(LOG_TAG, ex.toString());
}
return null;
}
import java.io.*;
import java.util.*;
import java点虐 .InetAddress;
public class Ip{
static public HashMap ping; //ping 后的结果集
public HashMap getPing(){ //用来得到ping后的结果集
return ping;
}
//当前线程的数量, 防止过多线程摧毁电脑
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
public void Ping(String ip) throws Exception{
//最多30个线程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){ //对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
//等着所有Ping结束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=(String)entry.getValue();
if(value.equals("true"))
System.out.println(key+"--"+value);
}
}
class PingIp extends Thread{
public String ip; // IP
public PingIp(String ip){
this.ip=ip;
}
public void run(){
try{
Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
//读取结果行
for (int i=1 ; i 7; i++)
input.readLine();
String line= input.readLine();
if (line.length() 17 || line.substring(8,17).equals("timed out"))
ping.put(ip,"false");
else
ping.put(ip,"true");
//线程结束
threadCount -= 1;
}catch (IOException e){}
}
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款