① java 代碼ping ip 1-255都能ping通
沒有你說的問題:
packageLianxi.yong2;
importjava.io.IOException;
importjava.net.ConnectException;
importjava.net.InetAddress;
importjava.net.UnknownHostException;
publicclassTest22{
publicstaticvoidmain(String[]args){
Stringip="192.168.10.";
for(inti=0;i<=255;i++){
InetAddressn;
try{
n=InetAddress.getByName(ip+i);
if(n.isReachable(1)){
System.out.println(ip+i);
}
}catch(UnknownHostExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ConnectExceptione){
//TODOAuto-generatedcatchblock
System.out.println("==============");
System.out.println(ip+i);
System.out.println("==============");
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
/*
==============
192.168.10.0
==============
java.net.ConnectException:Permissiondenied
atjava.net.Inet6AddressImpl.isReachable0(NativeMethod)
atjava.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:77)
atjava.net.InetAddress.isReachable(InetAddress.java:441)
atjava.net.InetAddress.isReachable(InetAddress.java:400)
atLianxi.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:Permissiondenied
atjava.net.Inet6AddressImpl.isReachable0(NativeMethod)
atjava.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:77)
atjava.net.InetAddress.isReachable(InetAddress.java:441)
atjava.net.InetAddress.isReachable(InetAddress.java:400)
atLianxi.yong2.Test22.main(Test22.java:15)
*/
② 用java怎麼實現調用cmd,並執行ping命令,求完整的語句
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
Process p = rt.exec("cmd /c ping www..com -t");
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String temp = null;
while((temp =br.readLine()) != null){
System.out.println(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
③ 用java寫個quick ping
簡單點的直接使用Runtime的exec來搞定,如下:
publicstaticvoidmain(String[]args)
{
StringBufferbuf=newStringBuffer();
Strings="";
Processprocess;
try
{
process=Runtime.getRuntime().exec("cmd/c"+"ping127.0.0.1");
BufferedReaderbr=newBufferedReader(newInputStreamReader(
process.getInputStream()));
while((s=br.readLine())!=null)
{
buf.append(s+" ");
}
process.waitFor();
System.out.println(buf);
}catch(Exceptionex)
{
ex.printStackTrace();
}
}
復雜點的,可以查看Oracle的官網文檔,實現了完整的ping:(粘貼下面的路徑到瀏覽器地址欄,加上http://)
docs.oracle.com/javase/1.5.0/docs/guide/nio/example/Ping.java
④ 用JAVA實現ping並給出詳細解釋
速度 = 距離 / 時間;
java 肯定是能實現的。前提需要移動設備,測量距離;
或者調用 XXX地圖的API,你自身有定位服務,能計算出距離;
⑤ 如何用java執行命令行
Java運行命令行並獲取返回值,下面以簡單的Java執行ping命令(ping 127.0.0.1 -t
)為例,代碼如下:
Processp=Runtime.getRuntime().exec("ping127.0.0.1-t");
Processp=Runtime.getRuntime().exec("javac");
InputStreamis=p.getInputStream();
BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
Stringline;
while((line=reader.readLine())!=null){
System.out.println(line);
}
p.waitFor();
is.close();
reader.close();
p.destroy();
}
⑥ 有誰知道怎樣使用java編寫ping程序
jpcap你要自己下好相應的包和配置,不知道的就在網上搜吧··
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.packet.EthernetPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
public class JPing {
private NetworkInterface[] devices = JpcapCaptor.getDeviceList();
private JpcapSender sender;
private JpcapCaptor jpcap;
private ICMPPacket icmpPacket;
private List<String> listResult = new ArrayList<String>();
/**
* 組織ICMP報文發送,並開啟線程接收報文
* @param ip
*/
public void ping(String ip) {
try {
jpcap = JpcapCaptor.openDevice(devices[0], 200, false, 20);
sender = jpcap.getJpcapSenderInstance();
jpcap.setFilter("icmp", true);// 過濾器,只接受ICMP報文
icmpPacket = new ICMPPacket();
icmpPacket.type = ICMPPacket.ICMP_ECHO; // 發送回顯請求報文
icmpPacket.setIPv4Parameter(0, false, false, false, 0, false,
false, false, 0, 1010101, 100, IPPacket.IPPROTO_ICMP,
devices[0].addresses[1].address, InetAddress.getByName(ip));
// 隨意的32bytes數據
icmpPacket.data = "".getBytes();
EthernetPacket ethernetPacket = new EthernetPacket();
ethernetPacket.frametype = EthernetPacket.ETHERTYPE_IP;
ethernetPacket.src_mac = devices[0].mac_address;
// 廣播地址
ethernetPacket.dst_mac = new byte[] { (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
icmpPacket.datalink = ethernetPacket;
listResult.add("Pinging " + icmpPacket.dst_ip + " with "
+ icmpPacket.data.length + " bytes of data: ");
startCapThread(jpcap);
for (int i = 0; i < 5; i++) {
icmpPacket.sec = 0;
//icmpPacket.usec = System.currentTimeMillis();
icmpPacket.usec = new GregorianCalendar().getTimeInMillis();// 記錄發送時間
icmpPacket.seq = (short) (1000 + i);
icmpPacket.id = (short) (999 + i);
sender.sendPacket(icmpPacket);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 接收ICMP報文
* @param jpcap
*/
public void getIcmpPacket(JpcapCaptor jpcapCaptor) {
try {
while (true) {
long tmp = 0;
String tmpStr = null;
ICMPPacket rp;
rp = (ICMPPacket) jpcapCaptor.getPacket();
if ((rp != null) && (rp.seq - rp.id == 1)
&& (rp.type == ICMPPacket.ICMP_ECHOREPLY)) {// 若是ICMP回應報文,則列出。。。
tmp = (rp.sec * 1000 + rp.usec / 1000 - icmpPacket.sec
* 1000 - icmpPacket.usec); // 計算發送與接受的時間差
if (tmp <= 0)
tmpStr = " < 1 ms ";
else
tmpStr = "= " + tmp + " ms ";
System.out.println("Reply from "
+ rp.src_ip.getHostAddress() + ": bytes = "
+ rp.data.length + " time " + tmpStr + "TTL = "
+ rp.hop_limit);
listResult.add("Reply from " + rp.src_ip.getHostAddress()
+ ": bytes = " + rp.data.length + " time " + tmpStr
+ "TTL = " + rp.hop_limit);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 接收ICMP報文
* @param jpcap
*/
public void startCapThread(final JpcapCaptor jpcap) {
Runnable runner = new Runnable() {
public void run() {
getIcmpPacket(jpcap);
}
};
new Thread(runner).start();
}
public static void main(String[] args) {
new JPing().ping("www..com");
}
}