我正在尝试使用jnetpcap
库编写一个捕获java程序的数据包。但我想知道我是否可以从给定端口获取PID?就我而言,我正在尝试使用Sigar API来获取PID(方法名称为getProcPort(protocol, port)
)。
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>(){
public void nextPacket(PcapPacket packet, String user) {
Tcp tcp = new Tcp();
Ip4 ip = new Ip4();
String protocol;
long port;
if(packet.hasHeader(ip)&&packet.hasHeader(tcp)){
protocol = tcp.getName();
port = tcp.source();
try {
//but the following line will cause an error
long pid = sigar.getProcPort("tcp", Long.toString(port));
System.out.println("pid : " + pid);
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(filtering==false){
printPacketResult(packet, tcp, ip, port, protocol);
}else if(filtering==true){
filteredPrintPacketResult(packet, tcp, ip, port, protocol);
}
}
}
};
我得到的错误是:
org.hyperic.sigar.SigarNotImplementedException:此方法没有 已经在这个平台上实现了 org.hyperic.sigar.SigarNotImplementedException。(SigarNotImplementedException.java:28) at org.hyperic.sigar.Sigar.getProcPort(Native Method)at org.hyperic.sigar.Sigar.getProcPort(Sigar.java:632)at networkInfo.PackageCapture $ 1.nextPacket(PackageCapture.java:84)at networkInfo.PackageCapture $ 1.nextPacket(PackageCapture.java:1)at org.jnetpcap.Pcap.loop(Native Method)at org.jnetpcap.Pcap.loop(未知来源)at networkInfo.PackageCapture.startNet(PackageCapture.java:111)at networkInfo.FilterChoice.choice(FilterChoice.java:27)at networkInfo.Main.main(Main.java:6)
有可能吗?如果你有一些信息,请给我一些提示。