📄 netstatus.java
字号:
package fr.umlv.projet.procfs;import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;import fr.umlv.projet.main.MainTest;/** * This class is to read the real time states of Net. * @author MA Xiao Jun & HUANG Wei * */public class NetStatus { private static float oldRecieve; private static float newRecieve; private static float oldTransmit; private static float newTransmit; private static float recieveSpeed; private static float transmitSpeed; private static int recieveTotal; private static int transmitTotal; private static int packRecieve; private static int packTransmit; private static String MAC; /** * load the states of Net from file "/proc/net/dev" */ public static void loadNetStatus(){ int packRec = 0; int packTran = 0; try { Scanner sc = new Scanner(new File("/proc/net/dev")); while(sc.hasNext ()){ String s = sc.next(); if(s.contains("eth")){ if(s.length()>8){ newRecieve += Float.parseFloat(s.substring(s.length()-8, s.length())); packRec += Integer.parseInt(sc.next()); packRecieve = packRec; for(int i=0;i<5;i++ ){ sc.next (); } String str = sc.next(); if(str.equals("0")){ newTransmit += Float.parseFloat(sc.next()); packTran += Integer.parseInt(sc.next()); packTransmit = packTran; } else{ newTransmit += Float.parseFloat(str.substring(s.length ()-8)); packTran += Integer.parseInt(sc.next()); packTransmit = packTran; } } else{ newRecieve += Float.parseFloat(sc.next()); packRec += Integer.parseInt(sc.next()); packRecieve = packRec; for(int i=0;i<6;i++ ){ sc.next(); } newTransmit += Float.parseFloat(sc.next()); packTran += Integer.parseInt(sc.next()); packTransmit = packTran; } } } calculRecieve(oldRecieve , newRecieve ); calculTransmit(oldTransmit , newTransmit); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * caculate value of data recieved. * @param oldRecieve the datas recieved before. * @param newRecieve the datas recieved real time. */ public static void calculRecieve(float oldRecieve,float newRecieve){ // System.out.println("oldRecieve "+oldRecieve+" newRecieve "+newRecieve); if(oldRecieve == 0){ NetStatus.oldRecieve = newRecieve; NetStatus.newRecieve = 0; } else{ float value = (newRecieve - oldRecieve)/(1024*(MainTest.getTime()/1000)); NetStatus.oldRecieve = newRecieve; recieveTotal = (int)newRecieve; NetStatus.newRecieve = 0; recieveSpeed = value;} } /** * caculate value of data Transmited. * @param oldTransmit the datas Transmited before. * @param newTransmit the datas Transmited real time. */ public static void calculTransmit(float oldTransmit , float newTransmit){ if(oldTransmit == 0){ NetStatus.oldTransmit = newTransmit ; NetStatus.newTransmit = 0; } else{ float value = (newTransmit - oldTransmit)/(1024*(MainTest.getTime()/1000)); NetStatus.oldTransmit = newTransmit ; transmitTotal = (int)newTransmit; NetStatus.newTransmit = 0; transmitSpeed = value; } } /** * load the address IP of computer. * @return address IP */ public static String loadIP(){ String s = ""; try { Scanner sc = new Scanner(new File("/proc/net/arp")); for(int i = 0 ;i<9 ; i++ ) if(sc.hasNext()){ sc.next(); } if(sc.hasNext()) s = sc.next(); return s; } catch (FileNotFoundException e) { e.printStackTrace(); } return s; } /** * load the address MAC of computer. * @return address MAC */ public static String loadMAC(){ String s = ""; try { Scanner sc = new Scanner(new File("/proc/net/arp")); for(int i = 0 ;i<12 ; i++ ) if(sc.hasNext()){ sc.next(); } if(sc.hasNext()) s = sc.next(); return s; } catch (FileNotFoundException e) { e.printStackTrace(); } return s; } /** * Return the speed of recieve data. * @return value of speed. */ public static float getRecieveSpeed() { return changeFloatSize(recieveSpeed,1); } /** * the speed of transmit data. * @return value of speed. */ public static float getTransmitSpeed() { return changeFloatSize(transmitSpeed,1); } /** * Change the size of float,and chang the type float to String. * @param value the value of float. * @param size the size of decimal fraction. * @return new value of number by means of String. */ public static float changeFloatSize(float value,int size){ String s = new String(value+""); int index = s.indexOf('.'); float num = 0.5f; if(s.length()>index+size+1){ if(s.charAt(index+size+1)>'4'){ for(int i=0;i<size;i++) num *= 0.1f; value += num; } s = new String(value+""); String s1 = s.substring(0,index+size+1); return Float.parseFloat(s1);} else return value; } // public static int getSpeed(){// return (recieveTotal + transmitTotal)/1024;// } /** * Return the size of total datas recieved. * @return the value of size of total datas. */ public static int getrecieveTotal(){ return recieveTotal/1024; } /** * Return the size of total datas transmited. * @return the value of size of total datas. */ public static int gettransmitTotal(){ return transmitTotal/1024; } /** * Return the number of packages recieved. * @return number of packages. */ public static int getPackRecieve() { return packRecieve; } /** * Return the number of packages recieved. * @return number of packages. */ public static int getPackTransmit() { return packTransmit; } /** * Return the address MAC. * @return address MAC. */ public static String getMAC(){ return MAC; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -