linux平台下,网络流量监控:
调用 result = Linux_Traffic("netstat -sw")
可以分析Linux下面的网络流量,其实就是在console下面敲入 netstat -sw,对返回的字符流进行分析
public long Linux_Traffic(String cmdline)
{
long result = 0 ;
try
{
String line;
Process p = Runtime.getRuntime().exec( cmdline );
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
int i = 0 ;
while ((line = input.readLine()) != null)
{
if ((line.indexOf("total packets received")!= -1) || (line.indexOf("requests sent out")!= -1))
{
line = line.trim() ;
int nSpaceIndex = line.indexOf(" ") ;
result += Long.parseLong(line.substring(0, nSpaceIndex)) ;
}
i ++ ;
}
input.close();
}
catch (Exception err)
{
return -1 ;
}
return result ;
}