java ping 实现的_纯java实现ICMP的ping命令

importjava.io.*;importjava.net.*;importjava.nio.channels.*;importjava.util.*;importjava.util.regex.*;publicclassPing {staticintDAYTIME_PORT=13;staticintport=DAYTIME_PORT;staticclassTarget {

InetSocketAddress address;

SocketChannel channel;

Exception failure;longconnectStart;longconnectFinish=0;booleanshown=false;

Target(String host) {try{

address=newInetSocketAddress(InetAddress.getByName(host),

port);

}catch(IOException x) {

failure=x;

}

}voidshow() {

String result;if(connectFinish!=0)

result=Long.toString(connectFinish-connectStart)+"ms";elseif(failure!=null)

result=failure.toString();elseresult="Timed out";

System.out.println(address+":"+result);

shown=true;

}

}staticclassPrinterextendsThread {

LinkedList pending=newLinkedList();

Printer() {

setName("Printer");

setDaemon(true);

}voidadd(Target t) {synchronized(pending) {

pending.add(t);

pending.notify();

}

}publicvoidrun() {try{for(;;) {

Target t=null;synchronized(pending) {while(pending.size()==0)

pending.wait();

t=(Target) pending.removeFirst();

}

t.show();

}

}catch(InterruptedException x) {return;

}

}

}staticclassConnectorextendsThread {

Selector sel;

Printer printer;

LinkedList pending=newLinkedList();

Connector(Printer pr)throwsIOException {

printer=pr;

sel=Selector.open();

setName("Connector");

}voidadd(Target t) {

SocketChannel sc=null;try{

sc=SocketChannel.open();

sc.configureBlocking(false);booleanconnected=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);

}

}voidprocessPendingTargets()throwsIOException {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);

}

}

}

}voidprocessSelectedKeys()throwsIOException {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);

}

}

}volatilebooleanshutdown=false;voidshutdown() {

shutdown=true;

sel.wakeup();

}publicvoidrun() {for(;;) {try{intn=sel.select();if(n>0)

processSelectedKeys();

processPendingTargets();if(shutdown) {

sel.close();return;

}

}catch(IOException x) {

x.printStackTrace();

}

}

}

}publicstaticvoidmain(String[] args)throwsInterruptedException,

IOException {

args=newString[] {"8888","192.168.10.193"};if(args.length<1) {

System.err.println("Usage: java Ping [port] host...");return;

}intfirstArg=0;if(Pattern.matches("[0-9]+", args[0])) {

port=Integer.parseInt(args[0]);

firstArg=1;

}

Printer printer=newPrinter();

printer.start();

Connector connector=newConnector(printer);

connector.start();

LinkedList targets=newLinkedList();for(inti=firstArg; i

Target t=newTarget(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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值