java实现ping的几种方式

余年寄山水
723次浏览
2020年07月28日 20:23
最佳经验
本文由作者推荐

almostlover-宰割的意思

功能(2008-11-19 15:34:54)转载标签: javapingit 分类: Java
一、纯Java实现ICMP的ping命令
import .*;
import .*;
import ls.*;
import .*;
import .*;
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(ame(host),
port);
} catch (IOException x) {
failure = x;
}
}
void show() {
String result;
if (connectFinish != 0)
result = ng(connectFinish - connectStart) + "ms";
else if (failure != null)
result = ng();
else
result = "Timed out";
n(address + " : " + result);
shown = true;
}
}
static class Printer extends Thread {
LinkedList pending = new LinkedList();
Printer() {
setName("Printer");
setDaemon(true);
}
void add(Target t) {
synchronized (pending) {
(t);
();
}
}
public void run() {
try {
for (;;) {
Target t = null;
synchronized (pending) {
while (() == 0)
();
t = (Target) First();
}
();
}
} 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 = ();
setName("Connector");
}
void add(Target t) {
SocketChannel sc = null;
try {
sc = ();
ureBlocking(false);
boolean connected = t(s);
l = sc;
tStart = tTimeMillis();
if (connected) {
tFinish = tStart;
();
(t);
} else {
synchronized (pending) {
(t);
}
se
();
}
} catch (IOException x) {
if (sc != null) {
try {
();
} catch (IOException xx) {
}
}
e = x;
(t);
}
}
void processPendingTargets() throws IOException {
synchronized (pending) {
while (() > 0) {
Target t = (Target) First();
try {
er(sel, _CONNECT, t);
} catch (IOException x) {
();
e = x;
(t);
}
}
}
}
void processSelectedKeys() throws IOException {
for (Iterator i = edKeys().iterator(); t();) {
SelectionKey sk = (SelectionKey) ();
();
Target t = (Target) ment();
SocketChannel sc = (SocketChannel) l();
try {
if (Connect()) {
();
tFinish = tTimeMillis();
();
(t);
}
} catch (IOException x) {
();
e = x;
(t);
}
}
}
volatile boolean shutdown = false;
void shutdown() {
shutdown = true;
();
}
public void run() {
for (;;) {
try {
int n = ();
if (n > 0)
processSelectedKeys();
processPendingTargets();
if (shutdown) {
();
return;
}
} catch (IOException x) {
tackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException,
IOException {
args = new String[] { "8888", "192.168.10.193" };
if ( < 1) {
n("Usage: java Ping [port] host...");
return;
}
int firstArg = 0;
if (s("[0-9]+", args[0])) {
port = nt(args[0]);
firstArg = 1;
}
Printer printer = new Printer();
();
Connector connector = new Connector(printer);
();
LinkedList tar
gets = new LinkedList();
for (int i = firstArg; i < i++) {
Target t = new Target(args[i]);
(t);
(t);
}
(2000);
wn();
();
for (Iterator i = or(); t();) {
Target t = (Target) ();
if (!)
();
}
}
}
二、JAVA调用外部EXE实现PING功能

import .*;
import .*;

public class Ping {


public Ping() {
}
public static void main(String args[])
{
if ( < 1)
{
n("syntax Error!");
}
else
{
String line = null;
try
{
Process pro = time().exec("ping " + args[0]);
BufferedReader buf = new BufferedReader(new InputStreamReader(utStream()));
while((line = ne()) != null)
n(line);
}
catch(Exception ex)
{
n(sage());
}
}

}
}
三、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 calls. 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 least
boolean status = ame(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 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.
四、最简单的办法,直接调用CMD
try
{
time().exec("cmd /c start ping 127.0.0.1");
}
catch (Exception ex)
{
n(sage());
}
ping的过程可以显示在本地的办法
import .*;
public class Ping
{
public static void main(String args[])
{
String line = null;
try
{
Process pro = time().exec("ping 127.0.0.1 ");
BufferedReader buf = new BufferedReader(
new InputStreamReader(utStream()));
while ((line = ne()) != null)
n(line);
}
catch (Exception ex)
{
n(sage());
}
}
}
五、模拟PING
利用InetAddress的isReachable方法可以实现ping的功能,里面参数设定超时时间,返回结果表示是否连上
try {
InetAddress address = ame("192.168.0.113");
n(hable(5000));
} catch (UnknownHostException e) {
tackTrace();
} catch (IOException e) {
tackTrace();
}
六、模拟TELNET
利用Socket的connect(SocketAddress endpoint, int timeout)方法可以实现telnet的功能,如果catch到异常说明telnet失败
try {
Socket server = new Socket();
InetSocketAddress address = new InetSocketAddress("192.168.0.113",
8080);
t(address, 5000);
();
} catch (UnknownHostException e){
n("telnet失败");
} catch (IOException e){
n("telnet失败");
}

七、httpclient
HttpClient client = new HttpClient();
GetMethod get = new GetMethod( "http://192.168.1.130:6666/axis2/services/RisForMlsService?wsdl" );
uthentication( true );
try {
int status = eMethod( get );
n(status+ "
" + ponseBodyAsString());
eConnection();
} catch (HttpException e) {
// TODO Auto-generated catch block
tackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
tackTrace();
}

三棱锥的体积-漂亮的读音


负一次方-瑕怎么读


摄氏度与华氏度-紫色土


荀组词-思量的意思


探组词-购销


因为你英文-蹙蹙


勒福特王水-返潮


陌上是什么意思-嗔怪的意思