In Java it is only possible to work with two types of sockets: stream based ones (or TCP ones - java.net.Socket andjava.net.ServerSocket) and datagram based ones (or UDP ones -java.net.DatagramSocket and java.net.MulticastSocket).
The open bug 4727550 asks to support other socket types, that will allow, for example, to perform an ICMP ping and, in general, to access the sockets in raw mode.

To implement an ICMP ping, a partial solution has been introduced in J2SE 5: a way to check if some address is reachable, viajava.net.InetAddress.isReachable() methods.
The implementation of these methods goes native and tries to do its best to "ping" the address represented by the InetAddress.

Surprisingly, there are many differences between the Windows and the Linux/Unix implementation ofjava.net.InetAddress.isReachable().

Windows, as strange as it seems, does not officially support an ICMP "ping" system call. The J2SE 5 implementation hence tries to open a TCP socket on port 7 (the echo service) and hopes to get some sort of reply.

Linux/Unix, instead, supports an ICMP "ping" system call. So the implementation of java.net.InetAddress.isReachable() first tries to perform the "ping" system call; if this fails, it falls back trying to open a TCP socket on port 7, as in Windows.

It turns out that in Linux/Unix the ping system call requires root privileges, so most of the times java.net.InetAddress.isReachable()will fail, because many Java programs are not run as root, and because the target address unlikely has the echo service up and running. 
Too bad.

But why on Linux, for example, I can use the ping program without being root ? 
It's because ping has the setuid bit set:

$> ls -l /bin/ping
-rwsr-xr-x 1 root root 30724 2005-11-11 01:15 /bin/ping

When a user invokes ping, it will run as root (see the "rws" permission for the owner ?).

But why on Windows there is a ping executable that does the job ?
It seems that this executable is relying on undocumented features, so everyone tries to avoid binding to those features.

So, for now, writing portable Java code that wants to send ICMP ping is quite an adventure.

ICMP and InetAddress.isReachable()的更多相关文章

  1. 解决InetAddress.isReachable(timeout)在windows xp始终返回false的bug

    笔者最近在做产品,其中一个环节用到ping测试主机是否在线. 开发环境:Windows 7 64bit+JDK1.8 x64 以下是检测主机是否在线,开发环境中测试通过 public static b ...

  2. android 8.0变更

    Android 8.0 行为变更 Android 8.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 其中大部分变 ...

  3. Android O新特性和行为变更总结zz

    https://mp.weixin.qq.com/s/Ezfm-Xaz3fzsaSm0TU5LMw Android O 行为变更https://developer.android.google.cn/ ...

  4. Coherence的集群成员的离开和加入机制研究

    最近在客户那里环境中coherence集群不稳定,所以找出一些文档,需要搞清楚Coherence内部的一些机制 1.集群成员的离开 关于状态的检测,官方的说法是: Death detection is ...

  5. android 8.0 适配(总结)

    android 8.0 对应的 sdk 版本  26 1. 通知栏 Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道.用户界面将通知渠道称之为通知类别. 针 ...

  6. Preferred Java way to ping a HTTP Url for availability

    I need a monitor class that regularly checks whether a given HTTP URL is available. I can take care ...

  7. ZooKeeper Administrator's Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)

    Deployment System Requirements Supported Platforms Required Software Clustered (Multi-Server) Setup ...

  8. ZooKeeper: 简介, 配置及运维指南

    1. 概览 ZooKeeper是一个供其它分布式应用程序使用的软件, 它为其它分布式应用程序提供所谓的协调服务. 所谓的协调服务, 是指ZooKeeper的如下能力 naming 命名 configu ...

  9. Java推断和检查网络

    在实践项目中.常常要处理网络异常等问题.为此,专门设计一个类,随时能够使用. import java.io.IOException; import java.net.InetAddress; impo ...

随机推荐

  1. 动态磁盘恢复为基本磁盘--DiskGenius

    近日在老电脑中安装了Win8.1,想不到使用起来比Win7还流畅. 周末,手贱,由于C盘只有10GB,为主分区,D盘有40GB,为扩展分区,想要将C.D两个分区合二为一,在Win8.1的磁盘管理器中, ...

  2. mysql数据库分组(GROUP BY)查询实例

    1.使用松散(Loose)索引扫描实现 GROUP BY 何谓松散索引扫描实现 GROUP BY 呢?实际上就是当 MySQL 完全利用索引扫描来实现 GROUP BY 的时候,并不需要扫描所有满足条 ...

  3. 第五十六篇、OC打开本地和网络上的word、ppt、excel、text等文件

    iOS打开本地和网络上的word.ppt.excel.text等文件 iOS开发过程中可能需要预览一些文件,这些文件的格式可能有word.ppt.excel等文件格式.那么系统提供两个类去预览这些文件 ...

  4. js画了一个椭圆

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. CSU-ACM2016暑假集训训练1-二分搜索 A - Can you find it?

    Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Give yo ...

  6. Kernel Panic常见原因以及解决方法

    Technorati 标签: Kernel Panic 出现原因 1. Linux在中断处理程序中,它不处于任何一个进程上下文,如果使用可能睡眠的函数,则系统调度会被破坏,导致kernel panic ...

  7. 你可以用OpenCV来干什么

    本文翻译自开源图书“OpenCV by Example”中第1章中的“What can you do with OpenCV?”小节. 使用OpenCV,你几乎可以做任何你能够想到的计算机视觉任务.现 ...

  8. winfrom LED时钟

    public sealed class Clock : PictureBox { public Clock() { SetStyle(ControlStyles.AllPaintingInWmPain ...

  9. Delphi2010中DataSnap技术网摘

    一.为DataSnap系统服务程序添加描述 这几天一直在研究Delphi 2010的DataSnap,感觉功能真是很强大,现在足有理由证明Delphi7该下岗了. DataSnap有三种服务模式,其中 ...

  10. CSS溢出文本省略(text-overflow)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...