我想用java程序实现输入一个域名163.com,得到和在终端里ping -c6 "163.com" 一样的输出结果,可是我的代码有误还是什么问题,反正就是什么也输出不了。请高手指点,代码如下。
public class Ping { /**
* get the IP addresses of a domian name by using "ping"
*
* @param domain
* the domain name
* @return IP addresses in a set. null if IOException thrown
*/
public static Set<String> getContentByPing(String domain) {
Set<String> cc = new HashSet<String>(3);
try {
Process p = Runtime.getRuntime().exec("ping " + domain);
InputStream is = p.getInputStream();
byte[] b=new byte[256];
int c;
String line = "";
while((c=is.read(b,0,256))!=-1){
line+=b;
cc.add(line);
}
p.destroy();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return cc;
}
public static void main(String[] args){
Set<String> ss=getContentByPing("163.com");
Iterator it=ss.iterator();
while(it.hasNext()){
System.out.println(it.next());
System.out.println("hello");
}
}
}
public class Ping { /**
* get the IP addresses of a domian name by using "ping"
*
* @param domain
* the domain name
* @return IP addresses in a set. null if IOException thrown
*/
public static Set<String> getContentByPing(String domain) {
Set<String> cc = new HashSet<String>(3);
try {
Process p = Runtime.getRuntime().exec("ping " + domain);
InputStream is = p.getInputStream();
byte[] b=new byte[256];
int c;
String line = "";
while((c=is.read(b,0,256))!=-1){
line+=b;
cc.add(line);
}
p.destroy();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return cc;
}
public static void main(String[] args){
Set<String> ss=getContentByPing("163.com");
Iterator it=ss.iterator();
while(it.hasNext()){
System.out.println(it.next());
System.out.println("hello");
}
}
}
解决方案 »
- java se 带惨方法这部分怎么去理解
- 关于按钮的action问题
- 高手进来讨论一下,这段简单Server端代码的性能问题?要连几千个Client端!!!
- 又是循环问题。。。
- java连接sqlserver200的问题,请有经验的指点一下
- 急!大家看看!!!
- 谁能提供给我一个sessionbean+entitybean的例子,jb做的,重分答谢
- 急!高人帮忙啊!当别人用Content-Type: text/plain; charset="ISO-8859-1"来发邮件时,我怎么查看啊?
- 求高手帮忙改一个“Java数据操作层”的类型的源码。
- String和StringBuilder的一些问题
- java 高手请进 帮小弟解决一下这个问题??????
- 有谁知道JAVA的刻度尺类有吗?或者把JSlider的小滑块去掉也行。请求高人出现。
line+=b;
cc.add(line);
}
要修改成while((c=is.read(b,0,256))!=-1){
line = new String(b);
cc.add(line);
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;public class HttpsTest
{
/**
* get the IP addresses of a domian name by using "ping"
*
* @param domain the domain name
* @return IP addresses in a set. null if IOException thrown
*/
public static List<String> getContentByPing(String domain)
{
List<String> cc = new ArrayList<String>(3);
try
{
Process p = Runtime.getRuntime().exec("ping " + domain);
BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = is.readLine()) != null)
{
cc.add(line);
}
p.destroy();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
return cc;
}
public static void main(String[] args)
{
List<String> ss = getContentByPing("163.com");
for(String line : ss)
{
System.out.println(line);
}
}
}
我自己也改好了
谢谢你啦
我是这样改的
package net;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TraceRoute {
/**
* get the IP addresses of a domian name by using "traceroute"
*
* @param domain
* the domain name
* @return IP addresses in a set. null if IOException thrown
*/
public static String getContentByPing(String domain) {
StringBuffer sb=new StringBuffer();
try {
String s;
Process p = Runtime.getRuntime().exec("traceroute " + domain);
InputStream is = p.getInputStream();
BufferedReader in=new BufferedReader(new InputStreamReader(is));
while((s=in.readLine())!=null)
sb.append(s+"\n");
in.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sb.toString();
}
public static void main(String[] args){
String ss=getContentByPing("163.com");
System.out.println(ss);
}
就是速度有点慢呢
import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;public class Ping { public static Set<String> getContentByPing(String domain) {
Set<String> cc = new HashSet<String>(3);
try {
Process p = Runtime.getRuntime().exec("ping " + domain);
InputStream is = p.getInputStream();
byte[] b = new byte[256];
int c;
PrintStream fos = new PrintStream(System.out);
while ((c = is.read(b)) != -1) {
fos.write(b, 0, c);
}
p.destroy();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return cc;
}
public static void main(String[] args) {
Set<String> ss = getContentByPing("163.com");
}
}