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 ...
随机推荐
- CI 更新字段
function update_click_num($brand_id) { $this->db->set('click_num', 'click_num+1', FALSE); $thi ...
- 【整理】c# 调用windows API(user32.dll)
User32.dll提供了很多可供调用的接口,大致如下(转自http://blog.csdn.net/zhang399401/article/details/6978803) using System ...
- OC2_点语法(属性关键字)
// // Dog.h // OC2_点语法(属性关键字) // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhang ...
- (转)实战Memcached缓存系统(1)Memcached基础及示例程序
1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...
- android 数据库的增删改查
主java package com.itheima.crud; import android.app.Activity; import android.content.Context; import ...
- Linux一
1,debian默认需要手动开启SSH连接# Authentication:LoginGraceTime 120PermitRootLogin without-passwordStrictModes ...
- Linux 信号量 生产者消费者小例题
菜鸟偶遇信号量,擦出火花(只有不熟才会有火花).于是上网搜资料和看<Unix环境高级编程>实现了几个小例题,高手请勿喷!这几位写得非常好啊: 题目来源: http://www.it165. ...
- zabbix短信接口调用
#!/bin/bash TIME=`date +%Y-%m-%d` KEY="UJK9rk50HD8du8JE8h87RUor0KERo5jk" username="za ...
- jQuery 全选 反选 单击行改变背景色
我先把CSS样式放出来,其实这个可以直接忽略 ;;font-size:12px;font-family:微软雅黑;} .datagrid{width:100%;} .datagird tr th{ba ...
- VS2010水晶报表的添加与使用
最近在学习VS2010水晶报表,发现原先安装的VS2010旗舰版没有 Crystal Report Viewer 控件,网上搜索一下发现要安装一个插件----CRforVS_13_0, 于是下载安装: ...