java实现ping的几种方式

更新时间:2023-08-12 01:30:31 阅读: 评论:0

功能(2008-11-19 15:34:54)转载标签: javapingit 分类: Java 
一、纯Java实现ICMP的ping命令
import java.io.*;
import java.*;
import java.nio.channels.*;
import java.util.*;producer
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 = fal;
Target(String host) {
try {
address = new ByName(host),
port);
} catch (IOException x) {
amelia earhart
failure = x;
}
}
void show() {
String result;
if (connectFinish != 0)
result = String(connectFinish - connectStart) + "ms";
el if (failure != null)
result = String();
el
result = "Timed out";
System.out.println(address + " : " + result);
shown = true;
notebook pc
}
}
static class Printer extends Thread {
LinkedList pending = new LinkedList();
Printer() {
tName("Printer");
tDaemon(true);
}
void add(Target t) {
synchronized (pending) {
cebpending.add(t);
}
}
public void run() {
try {
for (;;) {
Target t = null;
synchronized (pending) {
while (pending.size() == 0)
pending.wait();
t = (Target) veFirst();
}
t.show();
}
} catch (InterruptedException x) {
return;
}
}
}
static class Connector extends Thread {
Selector l;
Printer printer;
LinkedList pending = new LinkedList();
Connector(Printer pr) throws IOException {
printer = pr;
l = Selector.open();
tName("Connector");
}
void add(Target t) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
boolean connected = sc.connect(t.address);
t.channel = sc;
if (connected) {
sc.clo();
printer.add(t);
} el {
synchronized (pending) {
pending.add(t);
}
l.wakeup();
}
} catch (IOException x) {
if (sc != null) {
try {
sc.clo();
} catch (IOException xx) {
}
}
t.failure = x;
printer.add(t);
}
}
void processPendingTargets() throws IOException {
synchronized (pending) {
while (pending.size() > 0) {
Target t = (Target) veFirst();
try {
ister(l, SelectionKey.OP_CONNECT, t);
} catch (IOException x) {
t.channel.clo();
t.failure = x;
printer.add(t);
}
}
}
}
void processSelectedKeys() throws IOException {
for (Iterator i = l.lectedKeys().iterator(); i.hasNext();) {
SelectionKey sk = (SelectionKey) i.next();
Target t = (Target) sk.attachment();
SocketChannel sc = (SocketChannel) sk.channel();
try {
if (sc.finishConnect()) {
sk.cancel();
sc.clo();
printer.add(t);
}
} catch (IOException x) {
sc.clo();
t.failure = x;
printer.add(t);
}
}
}
volatile boolean shutdown = fal;
void shutdown() {
shutdown = true;
l.wakeup();
}
public void run() {
for (;;) {
try {
int n = l.lect();
if (n > 0)
processSelectedKeys();
processPendingTargets();
if (shutdown) {
l.clo();
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) {
darpa
return;
}
int firstArg = 0;
if (Pattern.matches("[0-9]+", args[0])) {
port = Integer.parInt(args[0]);
firstArg = 1;
}
Printer printer = new Printer();
printer.start();
Connector connector = new Connector(printer);
connector.start();
LinkedList tar
gets = 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();
}
}
}
二、JAVA调用外部EXE实现PING功能
import java.io.*;
hero 歌词import java.lang.*;
public class Ping {
public Ping() {
}
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("syntax Error!");
}
el
我亲爱的英文{
火药的英文
String line = null;
try
{
Process pro = Runtime().exec("ping " + args[0]);
BufferedReader buf = new BufferedReader(new InputStream()));
while((line = adLine()) != null)
System.out.println(line);
}
catch(Exception ex)
{
System.out.Message());
}
}
}
}
三、ICMP Ping in Java(JDK 1.5 and above)
Programatically using ICMP Ping is a great way to establish that a rver 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 conds at least
boolean status = ByName(host).isReachable(timeOut)
status is true if the machine is reachable by ping; fal otherwi. Best effort is made to try to reach the host, but firewalls and rver configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will u ICMP ECHO REQUE
STs if the privilege can be obtained, otherwi 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
{
}
catch (Exception ex)
{
System.out.Message());
}
ping的过程可以显示在本地的办法
import java.io.*;
public class Ping
{
public static void main(String args[])
{
String line = null;
try
{
Process pro = Runtime().exec("ping 127.0.0.1 ");
BufferedReader buf = new BufferedReader(
new InputStream()));
while ((line = adLine()) != null)
System.out.println(line);
fighting什么意思
}
catch (Exception ex)
{
System.out.Message());
}
}
}
五、模拟PING
利用InetAddress的isReachable方法可以实现ping的功能,里面参数设定超时时间,返回结果表示是否连上
try {
InetAddress address = ByName("192.168.0.113");
System.out.println(address.isReachable(5000));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
六、模拟TELNET
利用Socket的connect(SocketAddress endpoint, int timeout)方法可以实现telnet的功能,如果catch到异常说明telnet失败
try  {
Socket rver = new Socket();
InetSocketAddress address = new InetSocketAddress("192.168.0.113",
8080);
rver.clo();
} catch (UnknownHostException e){
System.out.println("telnet失败");
} catch (IOException e){
System.out.println("telnet失败");欣慰英文
}
七、httpclient
HttpClient client = new HttpClient();
GetMethod get = new GetMethod( "192.168.1.130:6666/axis2/rvices/RisForMlsService?wsdl" );
get.tDoAuthentication( true );
try {
int status = uteMethod( get );
System.out.println(status+ "\n" + ResponBodyAsString());
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

本文发布于:2023-08-12 01:30:31,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/194177.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:实现   功能   返回   时间   显示   方法
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图