成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

Java如何處理ARP報文的收發

2010-08-28 10:48:52來源:西部e網作者:

      前言

  java是跨平臺語言,一般來說對網絡的操作都在IP層以上,也就是只能對tcp/udp進行操作,當然也可以設置部分tcp/udp的option,如果想再往IP層或者數據link層操作就無能為力了,必須依靠jni使用本地OS的socket部分接口。很幸運,我在知道有winpcap的時候同時也知道有人在開發jpcap,此包可以方便的操作網絡底層應用協議,以下詳細描述。

  實施步驟

  下載需要的包:http://netresearch.ics.uci.edu/kfujii/jpcap/doc/index.html上可以下到最新的jpcap,你只需要把lib中的dll文件拷貝到jre的bin目錄,同時lib中的jar文件拷貝到jre中的lib/ext目錄下就安裝完整,當然你可以使用exe安裝包進行安裝,這樣會更加的簡單。

  編碼

  你可以使用任何你喜歡的ide工具,但是必須把jpcap.jar加到classpath中,否則無法編譯通過,以下為代碼詳細。

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;

import jpcap.*;
import jpcap.packet.*;

public class ARP {
 public static byte[] arp(InetAddress ip) throws java.io.IOException{
  //發現本機器的網絡接口
  int i;
  NetworkInterface[] devices=JpcapCaptor.getDeviceList();
  NetworkInterface device=null;
  for (i = 0; i < devices.length; i++)
  {
   System.out.println(devices[i].description);
  }
  device = devices[2];//我的機器是第三個進行網絡通信

  if(device==null)
   throw new IllegalArgumentException(ip+" is not a local address");

  //開啟網絡接口
  JpcapCaptor captor=JpcapCaptor.openDevice(device,2000,false,3000);
  captor.setFilter("arp",true);
  JpcapSender sender=captor.getJpcapSenderInstance();

  InetAddress srcip=null;
  //獲得接口地址,這里考慮到一網絡接口可能有多地址
  for(i = 0; i < device.addresses.length ; i++)
   if(device.addresses[i].address instanceof Inet4Address){
    srcip=device.addresses[i].address;
    break;
  }

  //填寫全1廣播mac目標地址
  byte[] broadcast=new byte[]{(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255};
  ARPPacket arp=new ARPPacket();
  arp.hardtype=ARPPacket.HARDTYPE_ETHER;
  arp.prototype=ARPPacket.PROTOTYPE_IP;
  arp.operation=ARPPacket.ARP_REQUEST;
  arp.hlen=6;
  arp.plen=4;
  arp.sender_hardaddr=device.mac_address;
  arp.sender_protoaddr=srcip.getAddress();
  arp.target_hardaddr=broadcast;
  arp.target_protoaddr=ip.getAddress();

  EthernetPacket ether=new EthernetPacket();
  ether.frametype=EthernetPacket.ETHERTYPE_ARP;
  ether.src_mac=device.mac_address;
  ether.dst_mac=broadcast;
  arp.datalink=ether;
  //發送
  sender.sendPacket(arp);
  //處理回復
  while(true){
   ARPPacket p=(ARPPacket)captor.getPacket();
   if(p==null){
    throw new IllegalArgumentException(ip+" is not a local address");
   }
   if(Arrays.equals(p.target_protoaddr,srcip.getAddress())){
    return p.sender_hardaddr;
   }
  }
 }

 public static void main(String[] args) throws Exception{
  int i;
  if(args.length<1){
   System.out.println("Usage: java ARP <ip address>");
  }else{
   byte[] mac=ARP.arp(InetAddress.getByName(args[0]));
   for (i = 0;i < mac.length; i++)
    System.out.print(Integer.toHexString(mac[i]&0xff) + ":");
   System.out.println();
   System.exit(0);
  }
 }
}

  運行:java ARP <ipaddr>

  本程序經過測試,在linux和win都可以正常運行。
關鍵詞:JavaARP

贊助商鏈接:

主站蜘蛛池模板: 上林县| 漾濞| 天等县| 侯马市| 红河县| 宝坻区| 逊克县| 台安县| 连州市| 阳东县| 商都县| 陇西县| 上虞市| 西吉县| 巧家县| 宜昌市| 湖州市| 榆中县| 探索| 瑞昌市| 克东县| 广德县| 绩溪县| 大田县| 棋牌| 泾阳县| 澄迈县| 化德县| 东丽区| 甘孜| 泾源县| 甘南县| 广西| 台前县| 丰顺县| 克什克腾旗| 兴化市| 灵台县| 额敏县| 新邵县| 英德市|