使用Jpcap实现网络监控的Demo

如题... 如题 展开
 我来答
匿名用户
2013-05-23
展开全部
public class TestJpcap {
public static void main(String[] args) throws IOException {
SendPacket();
} // 获取网络接口列表
public static void Demo() {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
for (int i = 0; i < devices.length; i++) {
System.out.println(i + ": " + devices[i].name + "("
+ devices[i].description + ")");
System.out.println(" datalink: " + devices[i].datalink_name + "("
+ devices[i].datalink_description + ")");
System.out.print(" MAC address:");
for (byte b : devices[i].mac_address)
System.out.print(Integer.toHexString(b & 0xff) + ":");
System.out.println();
for (NetworkInterfaceAddress a : devices[i].addresses)
System.out.println(" address:" + a.address + " " + a.subnet
+ " " + a.broadcast);
}
} // 使用回调方法, 从网络接口捕获数据包
public static void CallBackReceiver() throws IOException {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
int index = 1;
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
false, 20);
captor.processPacket(20, new PacketPrinter());
// captor.loopPacket(20, new PacketPrinter());
captor.close();
} // 使用逐个捕获方法, 从网络接口捕获数据包
public static void OneByOneReceiver() throws IOException {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
int index = 1;
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
false, 20);
// 设置过滤器
captor.setFilter("ip and tcp", true);
for (int i = 0; i < 10; i++)
System.out.println(captor.getPacket());
} //
public static void PacketWriteFile() throws IOException {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
int index = 1;
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
false, 20);
JpcapWriter writer = JpcapWriter.openDumpFile(captor, "yanrong");
for (int i = 0; i < 10; i++)
writer.writePacket(captor.getPacket());
writer.close();
} //
public static void PacketReadFile() throws IOException {
JpcapCaptor captor = JpcapCaptor.openFile("yanrong");
Packet packet;
while (true) {
packet = captor.getPacket();
if (packet == null) // || packet == Packet.EOF)
break;
System.out.println(packet.toString());
}
} // 通过网络接口发送数据包
public static void SendPacket() throws IOException {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
JpcapSender sender = JpcapSender.openDevice(devices[1]);
TCPPacket p = new TCPPacket(12, 23, 56, 78, false, false, false, false,
true, true, true, true, 10, 10);
p.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0,
1010101, 100, IPPacket.IPPROTO_TCP, InetAddress
.getByName("www.baidu.com"), InetAddress
.getByName("localhost"));
p.data = ("data").getBytes();
EthernetPacket ether = new EthernetPacket();
ether.frametype = EthernetPacket.ETHERTYPE_IP;
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 };
p.datalink = ether;
sender.sendPacket(p);
sender.close();
System.out.println(p);
}
}/** 使用回调方法从网络获取数据包时用到 */
class PacketPrinter implements PacketReceiver { @Override
public void receivePacket(Packet packet) {
System.out.println(packet.toString());
}
} 你看看~~
Storm代理
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式