BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #17613同步于 2011/3/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

java如何处理icmp报文的收发

gghwcf
2011/3/22镜像同步1 回复
从网上找了个代码,但在运行的时候老不行,不知道该怎么搞,如果在命令行里面输入java ICMP却显示Usage:javaICMP;应该怎样才能往下运行,代码里面的ip地址和mac地址我都修改了,ICMPPacket的seq和id不知道要不要修改,菜鸟求解答,谢谢! import java.net.InetAddress; import jpcap.JpcapCaptor; import jpcap.JpcapSender; import jpcap.NetworkInterface; import jpcap.packet.EthernetPacket; import jpcap.packet.ICMPPacket; import jpcap.packet.IPPacket; class ICMP { public static void main(String[] args) throws java.io.IOException { NetworkInterface[] devices = JpcapCaptor.getDeviceList(); if (args.length < 1) { System.out .println("Usage: java ICMP <device index (e.g., 0, 1..)> "); for (int i = 0; i < devices.length; i++) System.out.println(i + ":" + devices[i].name + "(" + devices[i].description + ")"); System.exit(0); } int index = Integer.parseInt(args[0]); // 开启网络设备 JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 2000, false, 3000); // 设置只过滤 icmp包 captor.setFilter("icmp", true); JpcapSender sender = captor.getJpcapSenderInstance(); ICMPPacket p = new ICMPPacket(); p.type = ICMPPacket.ICMP_ECHO; p.seq = (short) 0x0005; p.id = (short) 0x0006; p.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 1010101, 100, IPPacket.IPPROTO_ICMP, InetAddress .getByName("118.229.176.56"), InetAddress .getByName("118.229.176.37")); p.data = "abcdefghijklmnopqrstuvwabcdehghi".getBytes(); EthernetPacket ether = new EthernetPacket(); ether.frametype = EthernetPacket.ETHERTYPE_IP; // 填写自己和对方的mac地址,必须要正确填写,如果有错误将无法收到回包 ether.dst_mac = new byte[] { (byte) 0x00, (byte) 0x1D, (byte) 0x72, (byte) 0x4D, (byte) 0x58, (byte) 0x12 }; ether.src_mac = new byte[] { (byte) 0x00, (byte) 0x16, (byte) 0xD3, (byte) 0xBD, (byte) 0x6c, (byte) 0xA0 }; p.datalink = ether; sender.sendPacket(p); System.out.println("send..."); ICMPPacket rp = null; while (true) { rp = (ICMPPacket) captor.getPacket(); if (rp == null) { throw new IllegalArgumentException("no rcv icmp echo reply"); } else { System.out.println("rcv icmp echo reply"); return; } } } }
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
gghwcf机器人#1 · 2011/3/23
没人理啊,悲剧