1. Implement ICMP ping command in Java only
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(); } }}
Ii. Java calls external EXE to implement 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()); } } }}
Iii. ICMP ping in Java (JDK 1.5 and above)
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 CILS. Here is a simple and reliable method to do ICMP pings in Java without using JNI or NiO.
String host = "172.16.0.2"int timeOut = 3000; // I recommend 3 seconds at leastboolean status = InetAddress.getByName(host).isReachable(timeOut)
Status is true if the machine is reachable by Ping; false otherwise. best effort is made to try to reach the host, but firewalland 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.
4. The simplest way is to directly call cmd
try { Runtime.getRuntime().exec("cmd /c start ping 127.0.0.1"); } catch (Exception ex) { System.out.println(ex.getMessage()); }
The Ping process can be displayed locally.
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()); } }}
5. Simulate Ping
The isreachable method of inetaddress can be used to implement the Ping function. The parameter in the Ping function sets the timeout time and the returned result indicates whether the connection is successful.
try { InetAddress address = InetAddress.getByName("192.168.0.113"); System.out.println(address.isReachable(5000)); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Vi. Simulate Telnet Use the socket connect (socketaddress endpoint, int timeout) method to implement the Telnet function. if an exception is caught, Telnet fails.
Try {socket server = new socket (); inetsocketaddress address = new inetsocketaddress ("192.168.0.113", 8080); server. connect (addresses, 5000); server. close ();} catch (unknownhostexception e) {system. out. println ("Telnet failed");} catch (ioexception e) {system. out. println ("Telnet failed ");}
From: http://blog.sina.com.cn/s/blog_4b00fd1b0100by7z.html