「java对接ping」java对接票易通

博主:adminadmin 2022-11-23 19:47:07 74

本篇文章给大家谈谈java对接ping,以及java对接票易通对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何在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];

}

如何用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的功能...

可以用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代码调用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对接票易通、java对接ping的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。