我想用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");
}
}
}

解决方案 »

  1.   

    这个循环有问题while((c=is.read(b,0,256))!=-1){ 
    line+=b; 
        cc.add(line); 

    要修改成while((c=is.read(b,0,256))!=-1){ 
    line = new String(b); 
        cc.add(line); 

      

  2.   

    并且你修改后打印的还不是真实的输出的顺序,最好把你的Set换成ArrayList,然后使用索引访问,修改后的如下:import java.io.BufferedReader;
    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);
            }
        }
    }
      

  3.   

    谢谢你!
    我自己也改好了 
    谢谢你啦 
    我是这样改的
    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);
    }
    就是速度有点慢呢
      

  4.   

    这样子就可以了,你试试package test;import java.io.IOException;
    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");
    }
    }