2010-08-26 48 views
4

我正在尋找一個Java庫,它可以讓我訪問原始以太網幀,以便讀取和發送它們。我的最終目標是創建一個BACnet以太網網絡掃描器。原始以太網的Java庫

請注意,我不是在尋找TCP \ IP。

任何人都知道這樣做的好圖書館?

回答

6

也許Jpcap可以提供幫助。請注意,有一個名稱相同的Sourceforge項目,但它似乎不是同一個項目。

下面是一個使用JPCAP發送一個TCP數據包和以太網幀一些示例代碼(從庫的教程):

編輯:示例代碼創建一個TCPPacket,但你可以創建一個普通Packet代替。

//open a network interface to send a packet to 
JpcapSender sender=JpcapSender.openDevice(devices[index]); 

//create a TCP packet with specified port numbers, flags, and other parameters 
TCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10); 

//specify IPv4 header parameters 
p.setIPv4Parameter(0,false,false,false,0,false,false,false,0,1010101,100,IPPacket.IPPROTO_TCP, 
    InetAddress.getByName("www.microsoft.com"),InetAddress.getByName("www.google.com")); 

//set the data field of the packet 
p.data=("data").getBytes(); 

//create an Ethernet packet (frame) 
EthernetPacket ether=new EthernetPacket(); 
//set frame type as IP 
ether.frametype=EthernetPacket.ETHERTYPE_IP; 
//set source and destination MAC addresses 
ether.src_mac=new byte[]{(byte)0,(byte)1,(byte)2,(byte)3,(byte)4,(byte)5}; 
ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10}; 

//set the datalink frame of the packet p as ether 
p.datalink=ether; 

//send the packet p 
sender.sendPacket(p); 

sender.close();