
正文
java代码pingip java代码平方怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java获取局域网ip地址 具体代码
import java.io.*;
import java.util.*;
import java.net.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){}
}
}
}
相关问答
Q1: java怎样实现ping的功能...
可以用InetAddressjava代码pingip的isReachable方法java代码pingip:
import java.net.InetAddress;
public class MainTest {
public static void main(String[] args) {
try {
int timeOut = 3000;
byte[] ip = new byte[] { (byte) 192, (byte) 168, (byte) 100, (byte) 151 };
int retry = 4;
InetAddress address = InetAddress.getByAddress(ip);
for (int i = 0; i retry; i++) {
if (address.isReachable(timeOut)) {
System.out.println(i + " OK");
} else {
System.out.println(i + " LOSS");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Q2: 如何在JAVA语言中调用PING命令
*
* Ping.java
*
* Created on 2007年7月3日, 下午2:44
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package pingtest;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author tommy
*/
public class Ping
{
public Ping()
{
for(int i =0;i255;i++)
result[i] = 0;
}
public void scan()
{
for(int i=70;i85;i++)
{
String tmp = exec + i;
try
{
Process pr = Runtime.getRuntime().exec(tmp);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while (true)
{
String s = br.readLine();
if(s==null)
break;
if (s.contains("TTL"))
result[i] = 1;
System.out.println(s);
}
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String args[])
{
Ping a = new Ping();
a.scan();
for(int i =0;i255;i++)
System.out.println("result["+i+"] = "+a.result[i]);
}
private static String exec = new String("cmd /c ping 100.3.3.");
public int[] result = new int[255];
}
Q3: java 代码ping ip 1-255都能ping通
没有你说的问题:
package Lianxi.yong2;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test22 {
public static void main(String[] args) {
String ip = "192.168.10.";
for (int i = 0; i = 255; i++) {
InetAddress n;
try {
n = InetAddress.getByName(ip + i);
if (n.isReachable(1)) {
System.out.println(ip+i);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnectException e) {
// TODO Auto-generated catch block
System.out.println("==============");
System.out.println(ip + i);
System.out.println("==============");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
==============
192.168.10.0
==============
java.net.ConnectException: Permission denied
at java.net.Inet6AddressImpl.isReachable0(Native Method)
at java.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:77)
at java.net.InetAddress.isReachable(InetAddress.java:441)
at java.net.InetAddress.isReachable(InetAddress.java:400)
at Lianxi.yong2.Test22.main(Test22.java:15)
192.168.10.100
192.168.10.105
192.168.10.107
192.168.10.109
==============
192.168.10.255
==============
java.net.ConnectException: Permission denied
at java.net.Inet6AddressImpl.isReachable0(Native Method)
at java.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:77)
at java.net.InetAddress.isReachable(InetAddress.java:441)
at java.net.InetAddress.isReachable(InetAddress.java:400)
at Lianxi.yong2.Test22.main(Test22.java:15)
*/
java代码pingip的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码平方怎么写、java代码pingip的信息别忘了在本站进行查找喔。





