寫一個程序來模擬網橋功能?原來是這樣寫的!

1、 寫一個程序來模擬網橋功能。

模擬實現網橋的轉發功能,以從文件中讀取幀模擬網橋從網絡中收到一幀,即從兩個文 件中讀入一系列幀,從第一個文件中讀入一幀然後從第二個文件中再讀入一幀,如此下去。 對每一幀,顯示網橋是否會轉發,及顯示轉發表內容。

要求:Windows 或 Linux 環境下運行,程序應在單機上運行。

分析:用程序模擬網橋功能,可以假定用兩個文件分別代表兩個網段上的網絡幀數據。而兩 個文件中的數據應具有幀的特徵,即有目的地址,源地址和幀內數據。程序交替讀入幀的數 據,就相當於網橋從網段中得到幀數據。 對於網橋來說,能否轉發幀在於把接收到的幀與網橋中的轉發表相比較。判斷目的地址後才決定是否轉發。由此可見轉發的關鍵在於構造轉發表。這裏轉發表可通過動態生成。

在這裏插入圖片描述
參考代碼
Main.java:運行時程序的入口

public class Main {

    public static void main(String[] args) {
        try {
            // 文件流,第一個文件
            File file1 = new File("C:/Users/DELL/Desktop/a.txt");
            Reader reader1 = new InputStreamReader(new FileInputStream(file1));
            // 文件流,第二個文件
            File file2 = new File("C:/Users/DELL/Desktop/b.txt");
            Reader reader2 = new InputStreamReader(new FileInputStream(file2));
            // 讀取開始
            int i = 0;
            char[] chars1 = new char[4];
            char[] chars2 = new char[4];
            // 交替讀取文件裏的數據(幀)
            while((i = reader1.read(chars1))!=-1) {
                String frame1 = new String(chars1);
                // 轉發
                FowardingTable.forward(new Frame(frame1));
                while((i = reader2.read(chars2))!=-1) {
                    String frame2 = new String(chars2);
                    // 轉發
                    FowardingTable.forward(new Frame(frame2));
                }
            }
            reader1.close();
            reader2.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Frame.java:幀

/**
 * 幀
 * 規定爲4個字符,格式爲:源地址(1)+目標地址(1)+數據(2)
 * @Author DELL
 * @create 2020/5/7 8:50
 */
public class Frame {

    public Frame(String frame) throws Exception {
        if(frame.length() != 4) {
            throw new Exception("幀的長度錯誤,應爲4");
        }
        this.source = frame.substring(0,1);
        this.target = frame.substring(1,2);
        this.data = frame.substring(2,4);
    }

    /**
     * 源地址
     */
    private String source;

    /**
     * 目標地址
     */
    private String target;

    /**
     * 數據
     */
    private String data;
    //省略get、set和toString方法
}

ForwardingTable.java:負責轉發

/**
 * 轉發表
 * @Author DELL
 * @create 2020/5/7 8:59
 */
public class FowardingTable {

    public static Set<String> address = new LinkedHashSet<>();

    public static void forward(Frame frame) {
        System.out.println("-------------------------");
        System.out.println("路由檢測到幀:"+frame.getFrame());
        // 路由表將地址存儲
        address.add(frame.getSource());
        // 目的地址與源地址相同
        if(frame.getTarget().equals(frame.getSource())) {
            System.out.println("目的地址與源地址相同,不轉發");
        } else {
            // 路由表中含有目標地址,進行轉發
            if (address.contains(frame.getTarget())) {
                System.out.println(frame.getSource() + "轉發到" + frame.getTarget() + "成功,數據爲" + frame.getData());
            } else {
                System.out.println("查找失敗,"+frame.getSource() + "轉發到除" + frame.getSource() + "以外的其他地址");
            }
        }
        // 輸入轉發表裏面的內容
        System.out.print("轉發表:");
        for(String addr:address) {
            System.out.print(addr+"  ");
        }
        System.out.println();
        System.out.println("-------------------------");
    }
}

程序含有詳細註釋,這裏就不詳細解釋了。以下是運行前提前準備的兩個文件


程序運行結果如下:

輪流從a.txt和b.txt讀取數據,第一個幀AA00不轉發,並將A加入路由表,第二個幀BACD,由於路由表有A,轉發成功,並將B加入路由表。第四個幀在路由表找不到C,因此進行廣播,其他幀同理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章