国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java如何處理ARP報文的收發

2019-11-18 11:33:19
字體:
來源:轉載
供稿:網友

  前言

  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都可以正常運行。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平潭县| 扶沟县| 冕宁县| 安平县| 伊宁市| 措勤县| 会同县| 绥芬河市| 林周县| 婺源县| 罗平县| 关岭| 额尔古纳市| 玛沁县| 乐至县| 会宁县| 新巴尔虎左旗| 深水埗区| 城口县| 怀仁县| 洛阳市| 南和县| 湖南省| 康定县| 天水市| 沧源| 五原县| 新平| 汕头市| 达日县| 广丰县| 惠东县| 盘锦市| 黎平县| 天门市| 华阴市| 九龙城区| 永吉县| 神池县| 永吉县| 香格里拉县|