首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中使用ICMP和traceroutes

问如何在Java中使用ICMP和traceroutes
EN

Stack Overflow用户
提问于 2010-04-13 15:26:20
回答 2查看 18.1K关注 0票数 7

Java没有用于ICMP和traceroute的原语。如何克服这一点?基本上,我正在构建应该在*nix和Windows中运行的代码,并且需要一段可以在这两个平台上运行的代码。

EN

回答 2

Stack Overflow用户

发布于 2013-01-23 07:16:41

以下是我今天编写的用Java“实现”trace route命令的代码。我只在windows上测试过,但它在Linux上应该也能工作,尽管有几个适用于Linux的traceroute工具,所以很可能需要一些检查这些程序的存在。

代码语言:javascript
复制
public class NetworkDiagnostics{
  private final String os = System.getProperty("os.name").toLowerCase();

  public String traceRoute(InetAddress address){
    String route = "";
    try {
        Process traceRt;
        if(os.contains("win")) traceRt = Runtime.getRuntime().exec("tracert " + address.getHostAddress());
        else traceRt = Runtime.getRuntime().exec("traceroute " + address.getHostAddress());

        // read the output from the command
        route = convertStreamToString(traceRt.getInputStream());

        // read any errors from the attempted command
        String errors = convertStreamToString(traceRt.getErrorStream());
        if(errors != "") LOGGER.error(errors);
    }
    catch (IOException e) {
        LOGGER.error("error while performing trace route command", e);
    }

    return route;
}
票数 4
EN

Stack Overflow用户

发布于 2016-04-19 03:17:05

您将需要jpcap library (也许SourceForge jpcap也可以工作),并使用ICMPPacket类来实现所需的功能。

下面是使用jpcap library 的Java traceroute implementation。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2627706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档