Java used the Ping class in the Java.nio package to implement ping before JDK1.5. This class was not found in the JDK1.7 version of the API documentation.
Java.nio Packet is a non-blocking stream IO processing, the java.io packet is the blocking stream IO processing. The first few days were the use of IO blocking streams to enable client-to-server communication.
In the socket, currently I know there are two people method can block the program, one is the input stream InputStream Read method, one is serversocket in the Accept
Method. There is a disadvantage in the C/S mode, when the client sends a socket to the server each time, the server will open a thread to manage, then 10,000 clients, the server
You need to open 10,000 threads, when the number of clients is large, blocking stream Io is obviously not suitable, then use non-blocking stream io, if there are 10,000 clients,
The server uses a non-blocking IO stream as long as 1000 threads can be managed, because the blocking stream Io is synchronous, non-blocking stream Io is asynchronous, and more about the blocking stream IO
With non-blocking flow io knowledge, I think I need the relevant books to study.
The Java implementation Ping can be implemented by calling the command-letter ping under the WinDOS system. The code is as follows:
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.inetaddress;import Java.net.unknownhostexception;public class Test {public static void ping (String host) { String line = null; try{Process Pro = Runtime.getruntime (). EXEC ("ping" + host); BufferedReader buf = new BufferedReader (New InputStreamReader (Pro.getinputstream (), "GBK")); while (line = Buf.readline ())! = null) {System.out.println (line); }}catch (Exception ex) {System.out.println (Ex.getmessage ()); }}/** * @param host */public static void ICMP (String host) {try {int timeOut = 3000 ; Boolean status = Inetaddress.getbyname (host). Isreachable (TimeOut); SYSTEM.OUT.PRINTLN ("Send packet:" + status); } catch (Unknownhostexception e) {e.printstacktrace (); } catch (IOException e) {E.printstackTrace (); }}/** * @param args */public static void main (string[] args) throws Unknownhostexception {Ping (Inetaddress.getbyname ("www.baidu.com"). Gethostaddress ()); ICMP (Inetaddress.getbyname ("www.baidu.com"). Gethostaddress ()); }}
From the code can be seen, I was ping Baidu, Baidu sent back the message is GBK format, not set will appear garbled phenomenon, there is a point is the InetAddress package Isreachable method, this method is described in the API documentation is this:
Test whether the address is accessible. The best implementation is to attempt to reach the host, but the firewall and server configuration may block requests that cause a non-reachable state, and some specific ports may be accessible. A typical implementation would use an ICMP echo request, which would attempt to establish a TCP connection on port 7 (ECHO) of the target host if it could get privileges.
The timeout value, in milliseconds, indicates the maximum time that an attempt should take. If the operation times out before obtaining an answer, the host is considered unreachable. A negative value will cause the throw.
In other words, this method basically uses port 7 of the target host to establish a TCP connection, basically not ping what things.
Attached to the JDK1.5 version of the previous Ping class implementation, interested can see.
Https://docs.oracle.com/javase/1.5.0/docs/guide/nio/example/index.html
Java's doubts about Ping