开发工具:
文件大小: 700kb
下载次数: 0
上传时间: 2011-05-12
详细说明: package com.pbn.oss.provision.ws.ping; import java.net.InetAddress; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; import jpcap.JpcapCaptor; import jpcap.JpcapSender; import jpcap.NetworkInterface; import jpcap.packet.EthernetPacket; import jpcap.packet.ICMPPacket; import jpcap.packet.IPPacket; public class JPing { private NetworkInterface[] devices = JpcapCaptor.getDeviceList(); private JpcapSender sender; private JpcapCaptor jpcap; private ICMPPacket icmpPacket; private List listResult = new ArrayList(); /** * 组织ICMP报文发送,并开启线程接收报文 * @param ip */ public void ping(String ip) { try { jpcap = JpcapCaptor.openDevice(devices[0], 200, false, 20); sender = jpcap.getJpcapSenderInstance(); jpcap.setFilter("icmp", true);// 过滤器,只接受ICMP报文 icmpPacket = new ICMPPacket(); icmpPacket.type = ICMPPacket.ICMP_ECHO; // 发送回显请求报文 icmpPacket.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 1010101, 100, IPPacket.IPPROTO_ICMP, devices[0].addresses[1].address, InetAddress.getByName(ip)); // 随意的32bytes数据 icmpPacket.data = "abcdefghijklmnopqrstuvwxyzabcdef".getBytes(); EthernetPacket ethernetPacket = new EthernetPacket(); ethernetPacket.frametype = EthernetPacket.ETHERTYPE_IP; ethernetPacket.src_mac = devices[0].mac_address; // 广播地址 ethernetPacket.dst_mac = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }; icmpPacket.datalink = ethernetPacket; listResult.add("Pinging " + icmpPacket.dst_ip + " with " + icmpPacket.data.length + " bytes of data: "); startCapThread(jpcap); for (int i = 0; i < 5; i++) { icmpPacket.sec = 0; //icmpPacket.usec = System.currentTimeMillis(); icmpPacket.usec = new GregorianCalendar().getTimeInMillis();// 记录发送时间 icmpPacket.seq = (short) (1000 + i); icmpPacket.id = (short) (999 + i); sender.sendPacket(icmpPacket); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 接收ICMP报文 * @param jpcap */ public void getIcmpPacket(JpcapCaptor jpcapCaptor) { try { while (true) { long tmp = 0; String tmpStr = null; ICMPPacket rp; rp = (ICMPPacket) jpcapCaptor.getPacket(); if ((rp != null) && (rp.seq - rp.id == 1) && (rp.type == ICMPPacket.ICMP_ECHOREPLY)) {// 若是ICMP回应报文,则列出。。。 tmp = (rp.sec * 1000 + rp.usec / 1000 - icmpPacket.sec * 1000 - icmpPacket.usec); // 计算发送与接受的时间差 if (tmp <= 0) tmpStr = " < 1 ms "; else tmpStr = "= " + tmp + " ms "; System.out.println("Reply from " + rp.src_ip.getHostAddress() + ": bytes = " + rp.data.length + " time " + tmpStr + "TTL = " + rp.hop_limit); listResult.add("Reply from " + rp.src_ip.getHostAddress() + ": bytes = " + rp.data.length + " time " + tmpStr + "TTL = " + rp.hop_limit); } } } catch (Exception e) { e.printStackTrace(); } } /** * 接收ICMP报文 * @param jpcap */ public void startCapThread(final JpcapCaptor jpcap) { Runnable runner = new Runnable() { public void run() { getIcmpPacket(jpcap); } }; new Thread(runner).start(); } public static void main(String[] args) { new JPing().ping("www.baidu.com"); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.