Java网络模拟器入门

时间:2015-02-07 17:32:31

标签: java simulator

我正在使用Java Network Simulator(JNS)模拟网络拓扑。我已经按照JNS下载文档中的教程进行了操作。以下是示例的代码:

package simulator_;
import java.awt.peer.TextComponentPeer;
import java.io.IOException;

import jns.Simulator;
import jns.element.DuplexInterface;
import jns.element.DuplexLink;
import jns.element.Interface;
import jns.element.Link;
import jns.element.Node;
import jns.trace.Event;
import jns.trace.Trace;
import jns.util.IPAddr;

public class simu {

    public static void main(String[] args) {
        Simulator sim=Simulator.getInstance();

        Node src=new Node("Source node");
        Node router=new Node("Router");
        Node dest=new Node("Destination node");
        sim.attach(src);
        sim.attach(router);
        sim.attach(dest);


        Interface src_iface=new DuplexInterface(new IPAddr(192,168,1,10));
        src.attach(src_iface);
        sim.attach(src_iface);

        Interface dest_iface=new DuplexInterface(new IPAddr(128,116,11,20));
        dest.attach(dest_iface);
        sim.attach(dest_iface);


        Interface route_iface192=new DuplexInterface(new IPAddr(192,168,1,1));
        Interface route_iface128=new DuplexInterface(new IPAddr(128,116,11,1));

        router.attach(route_iface192);
        router.attach(route_iface128);

        sim.attach(route_iface192);
        sim.attach(route_iface128);


        Link link_src_router=new DuplexLink(1000000,0.001);
        Link link_router_dest=new DuplexLink(64000,0.1);

        src_iface.attach(link_src_router,true);
        route_iface192.attach(link_src_router,true);
        sim.attach(link_src_router);

        route_iface128.attach(link_router_dest,true);
        dest_iface.attach(link_router_dest,true);
        sim.attach(link_router_dest);


        src.addDefaultRoute(src_iface);
        dest.addDefaultRoute(dest_iface);

        router.addRoute(new IPAddr(192,168,1,0),new IPAddr(255,255,255,0),
                        route_iface192);
        router.addRoute(new IPAddr(128,116,11,0),new IPAddr(255,255,255,0),
                        route_iface128);

        sim.run();


    }

}

我遇到的问题是当我调用sim.run()时,程序会抛出NullPointerException错误。我是JNS的新手。请指导我如何在上述代码的帮助下成功创建一个文件javis to Simulate。我正在使用Eclipse IDE。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试设置跟踪实例

sim.setTrace(新的JavisTrace(" output.txt"));