资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码怎样ping,编写java代码

java怎样实现ping的功能...

可以用InetAddress的isReachable方法:

创新互联服务项目包括柘荣网站建设、柘荣网站制作、柘荣网页制作以及柘荣网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,柘荣网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到柘荣省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

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();

}

}

}

如何在JAVA语言中调用PING命令

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;i 255;i++)

result[i] = 0;

}

public void scan()

{

for(int i=70;i 85;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;i 255;i++)

System.out.println( "result[ "+i+ "] = "+a.result[i]);

}

private static String exec = new String( "cmd /c ping 127.0.0.1 ");

public int[] result = new int[255];

}

如何用Java实现Ping

你应该看看api process 返回的是流,按照输出流的方法操作即可.公司上不去外网,有代理才行,所以没有ping 百度,ping的本机.

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class CallCmd {

public static void main(String[] args) {

BufferedReader br = null;

try {

Process p = Runtime.getRuntime().exec("ping 127.0.0.1");

br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = null;

StringBuilder sb=new StringBuilder();

while ((line = br.readLine()) != null) {

sb.append(line+"\n");

}

System.out.println(sb.toString());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

有谁知道怎样使用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 ListString listResult = new ArrayListString();

/**

* 组织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 = "abcdefghijklmnopqrstuvwxyzabcdef".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("");

}

}

Java代码调用cmd中的ping命令.如何获得ping返回的信息?

public static void main(String[] args) throws IOException, InterruptedException {

// 执行ping命令

String cmdPing = "ping 127.0.0.1";

Runtime run = Runtime.getRuntime();

Process process = run.exec(cmdPing);

process.waitFor();

BufferedReader br = new BufferedReader(new     InputStreamReader(process.getInputStream(), Charset.forName("GBK")));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(line);

}

}


网站栏目:java代码怎样ping,编写java代码
分享路径:http://cdkjz.cn/article/dssooeh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220