ICMP and InetAddress.isReachable()
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()的更多相关文章
- 解决InetAddress.isReachable(timeout)在windows xp始终返回false的bug
笔者最近在做产品,其中一个环节用到ping测试主机是否在线. 开发环境:Windows 7 64bit+JDK1.8 x64 以下是检测主机是否在线,开发环境中测试通过 public static b ...
- android 8.0变更
Android 8.0 行为变更 Android 8.0 除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 其中大部分变 ...
- Android O新特性和行为变更总结zz
https://mp.weixin.qq.com/s/Ezfm-Xaz3fzsaSm0TU5LMw Android O 行为变更https://developer.android.google.cn/ ...
- Coherence的集群成员的离开和加入机制研究
最近在客户那里环境中coherence集群不稳定,所以找出一些文档,需要搞清楚Coherence内部的一些机制 1.集群成员的离开 关于状态的检测,官方的说法是: Death detection is ...
- android 8.0 适配(总结)
android 8.0 对应的 sdk 版本 26 1. 通知栏 Android 8.0 引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道.用户界面将通知渠道称之为通知类别. 针 ...
- 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 ...
- ZooKeeper Administrator's Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)
Deployment System Requirements Supported Platforms Required Software Clustered (Multi-Server) Setup ...
- ZooKeeper: 简介, 配置及运维指南
1. 概览 ZooKeeper是一个供其它分布式应用程序使用的软件, 它为其它分布式应用程序提供所谓的协调服务. 所谓的协调服务, 是指ZooKeeper的如下能力 naming 命名 configu ...
- Java推断和检查网络
在实践项目中.常常要处理网络异常等问题.为此,专门设计一个类,随时能够使用. import java.io.IOException; import java.net.InetAddress; impo ...
随机推荐
- (二)Qt界面设计之菜单栏
1.添加菜单项 直接输入菜单项名称,然后回车即可. 2.为菜单项设置图标 在动作编辑器上,右键选择编辑,然后添加图标 如果想在图标栏添加该菜单的快捷图标,直接将该动作项拖放只图标栏即可. 3.事件响应 ...
- 分布式系统怎样体现了CAP
`references:` 1. http://zh.wikipedia.org/wiki/CAP%E5%AE%9A%E7%90%86 2. http://en.wikipedia.org/wiki/ ...
- Oracle常见的问题
1.oracle 中文问号乱码解决方法: 在系统变量里面添加变量名:NLS_LANG 变量值: SIMPLIFIED CHINESE_CHINA.ZHS16GBK 2.查询oracle 里的主键 S ...
- ▲▲▲▲▲▲▲▲▲▲▲yum源的配置(本地和ftp)▲▲▲▲▲▲▲▲▲▲▲▲▲v
★★★★★★★★★★★★★★★本机yum源★★★★★★★★★★★★★★★★ 1. 首先把DVD里的OS镜像mount处理,如果插入光驱自动mount的话,一般在/media下面,比如RHEL_6.3 ...
- 锋利的qjuey-ajax
jquery 中的ajax load方法主要获取web服务器上静态数据 1 load方法载入HTML文档 load(url [,data] [,callback]) $(function(){ $ ...
- 前端工程模块化——以一个php项目为例
实现一个页面功能总是需要 JavaScript.CSS 和 Template 三种语言相互组织,所以我们真正需要的是一种可以将 JavaScript.CSS 和 Template 同时都考虑进去的模块 ...
- 10套免费的响应式布局 Bootstrap 模版
1. Cardio Cardio是我最喜欢的一个轻量级模板.它几乎可以很少的修改的用于任何类型的业务. 2. Evento Evento 是一个事件引导广告模板的形状.它是设计精美和注意细节. 3. ...
- [译]servlet3.0与non-blocking服务端推送技术
Non-blocking(NIO)Server Push and Servlet 3 在我的前一篇文章写道如何期待成熟的使用node.js.假定有一个框架,基于该框架,开发者只需要定义协议及相关的ha ...
- discuz X2.5自己写代码,获取当前登录的用户信息
<? //这个只是获取当前用户账号以及积分的方法,同样你修改SQL语句可以实现discuz所有数据处理的功能 require '../source/class/class_core.php';/ ...
- 【Vmware】已有镜像文件的导入
1 虚拟机文件夹中各个文件简介 在创建虚拟机的时候会把相关的文件保存到一个文件夹中.我的机器是Windows 7,64位 ,保存的路径是: C:\Users\User\Documents\Virtu ...