我必须IDS
为我的大学项目开发一个。我可以使用嗅探器的 java 代码和算法。我必须启用它以支持 1 GB 以太网流量/秒。为此,我们计划multi-threading
在双核机器上合并和运行代码。我打算在IP
. 程序的主要功能调用openInterface()
类packetLoader
{implements packetReciever
} 的方法。该方法openInterface()
打开NIC
界面并开始捕获数据包。我应该改变这种方法openInterface()
来合并multi-threading
吗?我应该在什么时候开始制作线程?我应该根据什么参数制作单独的线程?我应该如何实施所需的multi-threading
?
干杯:)
public void openInterface(String filter, int numOfPackets){
try {
if (!devName.startsWith(NIC_NAME_PREFIX)) {
if(numOfPackets == -1)
packetSamplingRatio = 1;
else {
packetSamplingRatio = numOfPackets/(double)totalPcapFilePackets;
}
}
//JpcapCaptor captor = null;
if (devName.startsWith(NIC_NAME_PREFIX)) {
System.err.println(".........inside openinterface");
NetworkInterface[] devicesList = JpcapCaptor.getDeviceList();
System.err.println(".........inside openinterface 2");
String nicName = devName.substring(NIC_NAME_PREFIX.length());
int nicID = -1;
for (int i = 0; i < devicesList.length; i++) {
System.err.println(".........inside openinterface 3");
if (devicesList[i].name.equals(nicName)){
System.err.println("Device no:" + i + "=" +devicesList[i].name);
System.err.println("capturing on device= " + devicesList[i].name);
nicID = i;}
}
if (nicID >= 0){
captor = JpcapCaptor.openDevice(devicesList[1],
NIC_SNAPLEN, true, NIC_TIMEOUT);
System.err.println(".........Device is open for packet capturing with");
System.err.println("NIC_SNAPLEN = " + NIC_SNAPLEN + " and NIC_TIMEOUT=" + NIC_TIMEOUT);
}
else {
System.err.println("Network interface " + nicName
+ "cannot be found!");
System.err.println("Availabel NICs:");
for(int k=0; k<devicesList.length; k++) {
System.out.println("- " + devicesList[k]);
}
System.exit(1);
}
} else {
System.err.println(".........inside else");
captor = JpcapCaptor.openFile(devName);
}
if (filter != null){
captor.setFilter(filter, true);
;
}// Start reading packets
System.err.println(".........filter checked");
//PacketStorage ps = new PacketStorage();
//captor.loopPacket(numOfPackets, this);
//captor.processPacket(numOfPackets, this);
for(int j =0; j<numOfPackets ; j++){
captor.getPacket();
System.err.println(".........captured packet" + j);
}
System.err.println(".........after capture.looppacket");
}
catch (IOException e) {
System.err.println("Exception in openDevice " + e);
System.exit(1);
}
}