- 浏览: 497044 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (247)
- java框架-struts2 (11)
- java框架-spring (6)
- 开发工具-myeclipse (8)
- 技术标准-webservice (1)
- 技术标准-opensaml2 (1)
- 开发语言-java (13)
- 开发语言-asp (4)
- 数据库-oracle (25)
- 数据库-mysql (4)
- 前端开发-css (15)
- 前端开发-javascript (34)
- 图片处理 (7)
- 事务处理 (1)
- SVN (2)
- 系统-linux (41)
- 系统-solaris (13)
- 系统-windows (8)
- web容器-glassfish (5)
- 单点登录-Shibboleth (0)
- 其他技术 (18)
- 技术无关 (6)
- 开发语言-jsp (2)
- 前端开发-html (1)
- LDAP相关 (11)
- 单点登录-cas (0)
- php (0)
- 门户-uportal (0)
- 单点登录-opensso (1)
- json (1)
- Android (2)
最新评论
-
wzwahl36:
http://www.atool.org/ico.php这个生 ...
Photoshop制作Favicon.ico图标(转载) -
wgimperial:
帮我解决了问题,谢谢LZ
如何让 href 失效(转) -
Finishx:
[color=olive][/color][size=medi ...
iframe跨域(转) -
q79969786:
不能删,上了这个workspace就是空的了
java.lang.NoClassDefFoundError: org/eclipse/core/resources/IContainer -
hw1287789687:
showrev
在SOLARIS下如何查看版本号
一、<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->纯Java 实现 ICMP 的 ping 命令
import java.io.*; import java.net.*; import java.nio.channels.*; import java.util.*; import java.util.regex.*; public class Ping { static int DAYTIME_PORT = 13; static int port = DAYTIME_PORT; static class Target { InetSocketAddress address; SocketChannel channel; Exception failure; long connectStart; long connectFinish = 0; boolean shown = false; Target(String host) { try { address = new InetSocketAddress(InetAddress.getByName(host), port); } catch (IOException x) { failure = x; } } void show() { String result; if (connectFinish != 0) result = Long.toString(connectFinish - connectStart) + "ms"; else if (failure != null) result = failure.toString(); else result = "Timed out"; System.out.println(address + " : " + result); shown = true; } } static class Printer extends Thread { LinkedList pending = new LinkedList(); Printer() { setName("Printer"); setDaemon(true); } void add(Target t) { synchronized (pending) { pending.add(t); pending.notify(); } } public void run() { try { for (;;) { Target t = null; synchronized (pending) { while (pending.size() == 0) pending.wait(); t = (Target) pending.removeFirst(); } t.show(); } } catch (InterruptedException x) { return; } } } static class Connector extends Thread { Selector sel; Printer printer; LinkedList pending = new LinkedList(); Connector(Printer pr) throws IOException { printer = pr; sel = Selector.open(); setName("Connector"); } void add(Target t) { SocketChannel sc = null; try { sc = SocketChannel.open(); sc.configureBlocking(false); boolean connected = sc.connect(t.address); t.channel = sc; t.connectStart = System.currentTimeMillis(); if (connected) { t.connectFinish = t.connectStart; sc.close(); printer.add(t); } else { synchronized (pending) { pending.add(t); } sel.wakeup(); } } catch (IOException x) { if (sc != null) { try { sc.close(); } catch (IOException xx) { } } t.failure = x; printer.add(t); } } void processPendingTargets() throws IOException { synchronized (pending) { while (pending.size() > 0) { Target t = (Target) pending.removeFirst(); try { t.channel.register(sel, SelectionKey.OP_CONNECT, t); } catch (IOException x) { t.channel.close(); t.failure = x; printer.add(t); } } } } void processSelectedKeys() throws IOException { for (Iterator i = sel.selectedKeys().iterator(); i.hasNext();) { SelectionKey sk = (SelectionKey) i.next(); i.remove(); Target t = (Target) sk.attachment(); SocketChannel sc = (SocketChannel) sk.channel(); try { if (sc.finishConnect()) { sk.cancel(); t.connectFinish = System.currentTimeMillis(); sc.close(); printer.add(t); } } catch (IOException x) { sc.close(); t.failure = x; printer.add(t); } } } volatile boolean shutdown = false; void shutdown() { shutdown = true; sel.wakeup(); } public void run() { for (;;) { try { int n = sel.select(); if (n > 0) processSelectedKeys(); processPendingTargets(); if (shutdown) { sel.close(); return; } } catch (IOException x) { x.printStackTrace(); } } } } public static void main(String[] args) throws InterruptedException, IOException { args = new String[] { "8888", "192.168.10.193" }; if (args.length < 1) { System.err.println("Usage: java Ping [port] host..."); return; } int firstArg = 0; if (Pattern.matches("[0-9]+", args[0])) { port = Integer.parseInt(args[0]); firstArg = 1; } Printer printer = new Printer(); printer.start(); Connector connector = new Connector(printer); connector.start(); LinkedList targets = new LinkedList(); for (int i = firstArg; i < args.length; i++) { Target t = new Target(args[i]); targets.add(t); connector.add(t); } Thread.sleep(2000); connector.shutdown(); connector.join(); for (Iterator i = targets.iterator(); i.hasNext();) { Target t = (Target) i.next(); if (!t.shown) t.show(); } } }<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
二、JAVA 调用外部 EXE 实现 PING 功能
import java.io.*; import java.lang.*; public class Ping { public Ping() { } public static void main(String args[]) { if (args.length < 1) { System.out.println("syntax Error!"); } else { String line = null; try { Process pro = Runtime.getRuntime().exec("ping " + args[0]); BufferedReader buf = new BufferedReader(new InputStreamReader( pro.getInputStream())); while ((line = buf.readLine()) != null) System.out.println(line); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } }<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
三、ICMP Ping in Java(JDK 1.5 and above)
<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
Programatically using ICMP Ping is a great way to establish that a server is up and running. Previously you couldn't do ICMP ping (what ping command does in Linux/Unix & Windows) in java without using JNI or exec calls. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NIO.
import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; public class Ping { public static void main(String args[]) { try { String host = "127.0.0.1"; int timeOut = 1000; // I recommend 3 seconds at least boolean status = InetAddress.getByName(host).isReachable(timeOut); System.out.println(status); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
status is true if the machine is reachable by ping; false otherwise. Best effort is made to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
In Linux/Unix you may have to suid the java executable to get ICMP Ping working, ECHO REQUESTs will be fine even without suid. However on Windows you can get ICMP Ping without any issues whatsoever.
<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
四、最简单的办法,直接调用CMD
import java.io.*; public class Ping { public static void main(String args[]) { String line = null; try { Process pro = Runtime.getRuntime().exec("ping 127.0.0.1 "); BufferedReader buf = new BufferedReader(new InputStreamReader(pro .getInputStream())); while ((line = buf.readLine()) != null) System.out.println(line); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
五、模拟PING
<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
利用InetAddress 的 isReachable 方法可以实现 ping 的功能,里面参数设定超时时间,返回结果表示是否连上
import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; public class Ping { public static void main(String args[]) { try { InetAddress address = InetAddress.getByName("127.0.0.1"); System.out.println(address.isReachable(5000)); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
六、模拟TELNET
<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
利用Socket 的 connect(SocketAddress endpoint, int timeout) 方法可以实现 telnet 的功能,如果 catch 到异常说明 telnet 失败
import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; public class Ping { public static void main(String args[]) { try { Socket server = new Socket(); InetSocketAddress address = new InetSocketAddress("127.0.0.1",80); server.connect(address, 5000); server.close(); System.out.println("telnet ok"); } catch (UnknownHostException e) { System.out.println("telnet失败"); } catch (IOException e) { System.out.println("telnet失败"); } } }
发表评论
-
java读取UTF-8文件
2012-08-07 11:51 1140import java.io.BufferedReader; ... -
java遍历目录(按类型查找+按文件名查找)
2012-07-12 14:02 1530import java.io.BufferedWriter; ... -
java写文件性能比较
2011-09-08 10:25 1151import java.io.File; import ja ... -
It is indirectly referenced from required .class file 问题解决(转)
2010-10-18 14:17 818原因: 你正要使用的类调用了另一个类,而这个类又调用了其 ... -
java路径问题总结(转)
2010-05-02 16:13 948一 相对路径的获得 说明:相对路径(即不写明时候到底 ... -
追加内容到文件末尾的几种常用方法
2010-05-02 16:11 828import java.io.BufferedWriter; ... -
8个你应该了解的正则表达式
2010-05-02 16:10 860正则表达式也可以被当作是一门语言,当你学习一门新的编程语言的时 ... -
java实现excel导入导出(1)(转)
2010-05-02 15:56 1947Java数据导入Excel package com.bo ... -
java实现excel导入导出(2)(转)
2010-05-02 15:56 2056=================摘要============ ... -
java实现excel导入导出(3)(转)
2010-05-02 15:54 4504JAVA EXCEL API简介 Java Excel是一开 ... -
Date中过期方法的替代品
2010-03-11 17:05 1884SimpleDateFormat formatter = ... -
异常: The Network Adapter could not establish the connection
2009-08-27 09:59 11362Io 异常: The Network Adapter coul ...
相关推荐
这个项目名为"Java实现ping功能",它利用Java编程语言,结合Spring Boot、Thymeleaf和Maven等技术,实现了类似操作系统内置ping命令的功能,并且增加了端口检测的特性。下面我们将详细探讨这一项目中的关键知识点。 ...
### 使用Java实现Ping的多种方法 在日常网络管理和软件开发中,经常需要检查网络连通性。`ping`命令作为一种简单而有效的工具被广泛应用于这一领域。本文将介绍几种使用Java来实现`ping`功能的方法。 #### 方法一...
标题“java实现ping.pdf”表明本文档内容与Java编程语言相关,特别是描述了一个实现网络诊断工具“ping”的Java程序。Ping工具主要用于测试数据包是否能够通过网络到达特定的主机,并测量往返时间(Round-Trip Time,...
资源包含:课程报告word+源码 编程实现PING的服务器端和客户端,实现操作系统提供的ping命令的类似功能。详细介绍参考:https://blog.csdn.net/sheziqiong/article/details/127039936
在Java编程中,我们不能直接使用内置的库来实现ping功能,因为Java标准库并不包含这样的功能。但是,我们可以借助第三方库如jpcap(Java Packet Capture)来实现这个功能。 jpcap是一个Java库,它提供了对网络接口...
- **`TestPingCmd`类**:这是整个程序的核心类,它包含了一些关键的方法来实现Ping功能。 - `main(String[] args)`方法:程序入口点,用于初始化并启动多线程处理任务。 - `getIpListFromTxt(String filename)`...
一段JAVA代码 实现ping功能 import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.nio.channels.SocketChannel;
总的来说,理解Java实现ping程序涉及到的知识点包括:网络基础知识、ICMP协议、Java套接字编程、UDP数据包的封装与解封,以及可能的JNI技术。通过这个过程,开发者可以深入理解网络通信的底层机制。
java中ping命令ping工具类(循环ping) java ping ip ping命令 ping工具类 支持linux和windows等所有平台 Ping是Windows下的一个命令 在Unix和Linux下也有这个命令。 ping也属于一个通信协议,是TCP/IP协议的一部分 ...
自动运维,只要把要PING的IP放在C盘的ip.txt中,格式每行一个IP地址即可。运行程序后,控制台会输出结果,显示通或者不通,也会把结果输出到C盘的result.txt,里面包括每个IP是否通,并且有哪一个日期时间去ping的。...
Ping程序是使用得比较多的用于测试网络连通性的程序。Ping程序基于ICMP协议,使用ICMP的回送请求和回送应答来工作。ICMP是基于IP的一个协议,ICMP包通过IP的封装之后传递。
设计服务器 PingServer 程序和客户端 PingClient 程序,使用 Java 网络编程中提供的 DatagramSocket,DatagramPacket,InetAddress 类及其内置方法实现服务器和客户端之间的 UDP 通信及数据报相关内容的显示,同时在...
Java实现ping功能主要涉及到对操作系统命令的调用和并发处理。在Java中,可以通过执行操作系统命令来实现ping操作,这通常使用`Runtime.getRuntime().exec()`或`ProcessBuilder`类来完成。下面详细解释这两种实现...
在Java编程语言中,实现ping功能通常涉及到网络通信和套接字编程。ping命令在网络中主要用于检查网络连接的可达性,其工作原理是发送ICMP(Internet Control Message Protocol)回显请求报文到目标主机,然后接收...
本文将深入探讨基于Socket实现Ping功能的源代码,涉及到的主要知识点包括Socket编程、原始套接字(SOCK_RAW)以及ICMP(Internet Control Message Protocol)协议。 首先,我们需要理解什么是Socket。Socket是操作...
3. **Java实现Ping**: - **Java ICMP库**:Java标准库并不直接支持ICMP,但可以借助第三方库如JICMP或JPing来实现。 - **Socket方式**:另一种方法是通过创建Socket并尝试连接到目标IP,虽然这不是真正的ICMP ...
【标题】"计网课设_Java实现简单的PING操作"涉及的主要知识点是计算机网络中的ICMP协议和Java编程语言的应用。在计算机网络中,PING是一个用于检查网络连接和数据包传输的基本工具,它基于Internet控制报文协议...
下面将详细解释基于UDP的PING实现以及相关的Java编程知识。 首先,UDP是传输层的一种无连接协议,它不提供诸如确认、流量控制或重传等服务,因此相对于TCP,UDP更适合于对实时性要求高但可以容忍数据丢失的应用场景...