也许Jpcap可以提供帮助。请注意,有一个具有相同名称的Sourceforge项目,但似乎不是同一项目。
这是一些示例代码(来自库的教程),该示例代码使用Jpcap发送TCP数据包和以太网帧:
编辑:示例代码确实创建了一个
TCPPacket,但是您可以创建一个常规
Packet。
//open a network interface to send a packet toJpcapSender sender=JpcapSender.openDevice(devices[index]);//create a TCP packet with specified port numbers, flags, and other parametersTCPPacket p=new TCPPacket(12,34,56,78,false,false,false,false,true,true,true,true,10,10);//specify IPv4 header parametersp.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 packetp.data=("data").getBytes();//create an Ethernet packet (frame)EthernetPacket ether=new EthernetPacket();//set frame type as IPether.frametype=EthernetPacket.ETHERTYPE_IP;//set source and destination MAC addressesether.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 etherp.datalink=ether;//send the packet psender.sendPacket(p);sender.close();


