java 以太网,原始以太网的Java库

I'm looking for a Java library that will give me access to raw Ethernet frames, both for reading and sending them. My end goal is to create a BACnet Ethernet network scanner.

Please, Note, I'm not looking for TCP\IP.

Anyone know of a good library for doing this?

解决方案

Maybe Jpcap can help. Notice that there's a Sourceforge project with the same name, but it doesn't seem to be the same project.

Here's some sample code (from the library's tutorial) that uses Jpcap to send a TCP packet and an Ethernet frame:

Edit: The sample code does create a TCPPacket, but you may create a regular Packet instead.

//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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值