-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketDriverExample.java
executable file
·30 lines (26 loc) · 1.24 KB
/
PacketDriverExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.nio.ByteBuffer;
public class PacketDriverExample
{
public static void main(String[] args) {
SimplePacketDriver driver=new SimplePacketDriver();
//Get adapter names and print info
String[] adapters=driver.getAdapterNames();
System.out.println("Number of adapters: "+adapters.length);
for (int i=0; i< adapters.length; i++) System.out.println("Device name in Java ="+adapters[i]);
//Open first found adapter (usually first Ethernet card found)
if (driver.openAdapter(adapters[0])) System.out.println("Adapter is open: "+adapters[0]);
//Read a packet (blocking operation)
byte [] packet=driver.readPacket();
//Wrap it into a ByteBuffer
ByteBuffer Packet=ByteBuffer.wrap(packet);
//Print packet summary
System.out.println("Packet: "+Packet+" with capacity: "+Packet.capacity());
System.out.println(driver.byteArrayToString(packet));
//Send the same packet now (change headers)
for (int i=0; i< 6; i++) packet[i]=1; //Destination
for (int i=0; i< 6; i++) packet[i+6]=2; //Source
packet[12]=9; packet[13]=10; //Make up a type
//Send packet
if (!driver.sendPacket(packet)) System.out.println("Error sending packet!");
}
}