请高手指点,难道java就不能实现Ping的功能吗? 我才看到用JAVA实现这个功能的http://www.chinajavaworld.com/进最上边的“JAVA例程/用JAVA程序取得IP地址”就可以看到了 快去吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是可以实现的,可惜我现在放假回家了,否则就可以贴篇文章给你看看,pity.....。 import java.io.*;import java.net.InetAddress;......... String s; try { while((s = in.readLine()).length() != 0) try{ InetAddress ip=InetAddress.getByName(s); System.out.println(s+"=="+ip.toString()+" host name="+ip.getHostName()+" path name="+ip.getHostName()); }catch(Exception e){System.out.println(s+" is 连接不上");} } catch(IOException e) { e.printStackTrace(); } 以下是我的测试结果,当某个ip不存在或者ping不通时显示连接不上:11.14.192.49==playgirl/11.14.192.49 host name=playgirl path name=playgirl11.14.192.141==11.14.192.141/11.14.192.141 host name=11.14.192.141 path name=11.14.192.14111.14.192.143 is 连接不上11.14.192.145==playboy/11.14.192.145 host name=playboy path name=playboy 我测试和你一样。import java.io.*;import java.net.*;public class PseudoPing { public static void main(String args[]) { try { Socket t = new Socket(args[0], 7); DataInputStream dis = new DataInputStream(t.getInputStream()); PrintStream ps = new PrintStream(t.getOutputStream()); ps.println("Hello"); String str = dis.readLine(); if (str.equals("Hello")) System.out.println("Alive!") ; else System.out.println("Dead or echo port not responding"); t.close(); } catch (IOException e) { e.printStackTrace();} } }但只能测出具有Http、Ftp等服务的机器, 你按照我的试试看是否只能测出具有Http、Ftp等服务的机器!因为我们这边很多都是me或2000的所以我也策不了没有具有Http、Ftp等服务的机器 你可以用这个试试: try { Process process = Runtime.getRuntime().exec("ping 172.17.60.45"); String output; BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while ( (output=bufferedReader.readLine()) != null) out.println(output); } catch (Exception e){ out.println("<b>Wrong selection : </b>" + e.toString()); } 下面程序和ping功能差不多,甚至比ping还要好,你先编译 然后 java InetAddressTest +ip地址或机器名/** * @version 1.00 1999-08-28 * @author Cay Horstmann */import java.net.*;public class InetAddressTest{ public static void main(String[] args) { try { if (args.length > 0) { String host = args[0]; InetAddress[] addresses = InetAddress.getAllByName(host); for (int i = 0; i < addresses.length; i++) System.out.println(addresses[i]); } else { InetAddress localHostAddress = InetAddress.getLocalHost(); System.out.println(localHostAddress); } } catch (Exception e) { System.out.println("Error: " + e); } }} 试试 TO:tiangou(分数不多就去灌水乐园) 请问路由器可用您的程序测试吗? 【提问】怎么通过程序让电脑发出报警声音 关于第一个小程序 wordcount的问题。。 关于串口 急!急!急!——求段代码——谁进来帮我看下,关于解析多文件。 Java 中 <> 的用法 如何判断从命令行输入的数值是不是某种基本类型? 关于win32com.dll 的版本问题 对一个list分组求和??? 我这个怎么不显示我想要的东西呀? 高分求解,谁能提供使用微软的jdbc接口制作的实例,必须可以调用数据的 如何读到以下内容? 怎样从java中导出一个excel文件??
import java.net.InetAddress;
......... String s;
try {
while((s = in.readLine()).length() != 0)
try{
InetAddress ip=InetAddress.getByName(s);
System.out.println(s+"=="+ip.toString()+" host name="+ip.getHostName()+" path name="+ip.getHostName());
}catch(Exception e){System.out.println(s+" is 连接不上");}
} catch(IOException e) {
e.printStackTrace();
}
11.14.192.49==playgirl/11.14.192.49 host name=playgirl path name=playgirl
11.14.192.141==11.14.192.141/11.14.192.141 host name=11.14.192.141 path name=11.14.192.141
11.14.192.143 is 连接不上
11.14.192.145==playboy/11.14.192.145 host name=playboy path name=playboy
import java.io.*;
import java.net.*;public class PseudoPing {
public static void main(String args[]) {
try {
Socket t = new Socket(args[0], 7);
DataInputStream dis = new DataInputStream(t.getInputStream());
PrintStream ps = new PrintStream(t.getOutputStream());
ps.println("Hello");
String str = dis.readLine();
if (str.equals("Hello"))
System.out.println("Alive!") ;
else
System.out.println("Dead or echo port not responding");
t.close();
}
catch (IOException e) {
e.printStackTrace();}
}
}
但只能测出具有Http、Ftp等服务的机器,
Process process = Runtime.getRuntime().exec("ping 172.17.60.45");
String output;
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));
while ( (output=bufferedReader.readLine()) != null)
out.println(output);
}
catch (Exception e){ out.println("<b>Wrong selection : </b>" + e.toString()); }
你先编译 然后 java InetAddressTest +ip地址或机器名
/**
* @version 1.00 1999-08-28
* @author Cay Horstmann
*/import java.net.*;public class InetAddressTest
{ public static void main(String[] args)
{ try
{
if (args.length > 0)
{ String host = args[0];
InetAddress[] addresses
= InetAddress.getAllByName(host);
for (int i = 0; i < addresses.length; i++)
System.out.println(addresses[i]);
}
else
{ InetAddress localHostAddress
= InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
}
catch (Exception e)
{ System.out.println("Error: " + e);
}
}
} 试试
请问路由器可用您的程序测试吗?