⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interfacelist.java

📁 DSr project for the java applications
💻 JAVA
字号:
package dsr;import java.net.*;import java.util.*;//this class implements the interface list. It stores the information about all the//network interfaces available to the OSpublic class InterfaceList {  static Vector interfaceList;     //this is the vector for the interface list  //constructor  public InterfaceList() {    interfaceList = new Vector(3, 2);    findInterface();  }  //this is the function for construcing the interface Entries from the interface information  //obtained from the OS  public static void createInterface(String s, int port) {    //string s contains the interface information    Vector token = new Vector(3,2);    //gets the words from the string s    StringTokenizer st = new StringTokenizer(s, " :;\n");    String tmp;    while (st.hasMoreTokens()){      tmp = st.nextToken();      token.addElement(tmp);    }    int index = 0, add = 1;    String tempName = "", tempIndex = "";    InetAddress tempIP;    //the folowing code goes through the words obtained from the string and    //extracts the interface name, its index and the ip address associated with it.    try {      tempIP = InetAddress.getByName("0.0.0.0");      if ((index = token.indexOf("name", index)) != -1) {        tempName = (String)token.elementAt(index+1);        if ((index = token.indexOf("index", index)) != -1) {          tempIndex = (String)token.elementAt(index+1);          if ((index = token.indexOf("addresses", index)) != -1) {            //gets the type of OS this application is running on            String osname = System.getProperties().getProperty("os.name");            System.out.println("You'are using " + osname + " operating system.");            tmp = (String)token.lastElement();            //depending on the operating system it extracts the address information            if (osname.startsWith("Windows")) {              if (tmp.length() > 1)                tempIP = InetAddress.getByName(tmp.substring(1, tmp.length()));              else                add = 0;            }            else if (osname.startsWith("SunOS")) {              tempIP = InetAddress.getByName(tmp.substring(1, tmp.length()));            }          }        }      }      //don't want to enter the loop back address.      if (!tempName.equals("lo0") && (add == 1) ) {        System.out.println(tempName + " " + tempIndex + " " + tempIP.toString());        try {          //creates the interface entry and adds it to the interface table          interfaceList.addElement(new InterfaceListEntry(tempIP, tempName, tempIndex, port));        } catch (NullPointerException e) {}        //adds the interface information in the routing table as a self route        addInterfaceToRoutingTable((InterfaceListEntry)interfaceList.lastElement());        add = 0;      }    } catch (UnknownHostException e) {}  }  //checks to see if the ip address is one of my interface addresses.  public static boolean checkIfInterfacePresent(InetAddress addr) {    Iterator i = interfaceList.iterator();    while (i.hasNext()) {      InterfaceListEntry tmp = new InterfaceListEntry((InterfaceListEntry)i.next());      if (tmp.ipAddress.equals(addr)) {        return true;      }    }    return false;  }  //adds an interface to the routing table. basically adds the self route  public static void addInterfaceToRoutingTable(InterfaceListEntry tmpEntry){    if (!RouteTable.IfEntryPresentInRouteTable(tmpEntry))      RouteTable.AddRouteTableEntry(new RouteTableEntry                                    (tmpEntry.ipAddress, tmpEntry.ipAddress, (byte)0, (byte)1));  }  //this is the function that initiates the discovery of the interfaces.  //it gets the network interfaces in the form of strings and then calls the  //createInterface() to extracts the interface information  public static void findInterface() {    try {      for (Enumeration e = NetworkInterface.getNetworkInterfaces() ; e.hasMoreElements() ;) {        String s;        s = e.nextElement().toString();        System.out.println(s);        createInterface(s, 6666);        System.out.println();      }    } catch (SocketException ei){}    //prints out the interface table    Iterator vItr = interfaceList.iterator();    System.out.println("\nElements in vector");    while (vItr.hasNext()){      System.out.println(((InterfaceListEntry)vItr.next()).toString());    }    /*    vItr = RouteTable.routingTable.iterator();    System.out.println("elements in routing table");    while(vItr.hasNext()) {      System.out.println(((RouteTableEntry)vItr.next()).toString());    }*/  }}/*  public static void main (String[] args){    try {      for (Enumeration e = NetworkInterface.getNetworkInterfaces() ; e.hasMoreElements() ;) {        String s;        s = e.nextElement().toString();        System.out.println(s);        createInterface(s, 6666);        System.out.println();      }    } catch (SocketException ei){}    Iterator vItr = interfaceList.iterator();    while (vItr.hasNext()){      System.out.println("\nElements in vector");      System.out.println(((InterfaceListEntry)vItr.next()).toString());    }    vItr = RouteTable.routingTable.iterator();    while(vItr.hasNext()) {      System.out.println("elements in routing table");      System.out.println(((RouteTableEntry)vItr.next()).toString());    }  }}    /*    Enumeration vEnum = interfaceList.elements();    System.out.println("\nElements in vector");    while (vEnum.hasMoreElements()) {     System.out.println(((InterfaceList)vEnum.nextElement()).toString());    }      // TODO: Add initialization code here  }}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -